作者:lastsweetop
搞了2小时 终于调试通过,贴出代码大家参考
需要导入的包
avalon-logkit-2.1.jar
commons-collections-3.2.jar
commons-digester-1.8.jar
commons-lang-2.1.jar
commons-logging-1.0.4.jar
commons-validator-1.3.0.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
spring-beans.jar
spring-context.jar
spring-core.jar
spring-web.jar
struts2-core-2.0.11.1.jar
struts2-spring-plugin-2.0.11.1.jar
velocity-1.5.jar
velocity-tools-1.4.jar
velocity-tools-view-1.4.jar
xwork-2.0.4.jar
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="sweetop" />
</beans>
ChatAction.java
package sweetop;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.opensymphony.xwork2.ActionSupport;
@Scope("prototype")
@Component
public class ChatAction extends ActionSupport {
private static final long serialVersionUID = 8445871212065L;
@Autowired
private ChatService chatService;
private Set<String> userNames;
public void setChatService(ChatService chatService) {
this.chatService = chatService;
}
public Set<String> getUserNames() {
return userNames;
}
@Override
public String execute() {
userNames = chatService.getUserNames();
return SUCCESS;
}
}
ChatService.java
package sweetop;
import java.util.Set;
import org.springframework.stereotype.Component;
public interface ChatService {
Set < String > getUserNames();
}
ChatServiceImpl.java
package sweetop;
import java.util.HashSet;
import java.util.Set;
import org.springframework.stereotype.Component;
@Component
public class ChatServiceImpl implements ChatService {
public Set<String> getUserNames() {
Set<String> users = new HashSet<String>();
users.add("Max");
users.add("Scott");
users.add("Bob");
return users;
}
}
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml" />
<package name="sweetop" extends="struts-default"><action name="Chat" class ="sweetop.ChatAction">
<result>/UserList.jsp</result>
</action>
</package>
</struts>
struts.properties
struts.objectFactory=spring
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>struts1</display-name>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>velocity</servlet-name>
<servlet-class>
org.apache.velocity.tools.view.servlet.VelocityViewServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
UserList.jsp
<%@ page contentType = "text/html; charset=UTF-8" %>
<%@ taglib prefix = "s" uri = "/struts-tags" %>
<html>
<head>
<title> User List </title>
</head>
<body>
<h2> User List </h2>
<ol>
<s:iterator value ="userNames">
<li><s:property /></li>
</s:iterator>
</ol>
</body>
</html>