struts 2.0 + spring 2.5 实现IOC 源代码

作者: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


 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">
 

 

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 userNames;
 
 public void setChatService(ChatService chatService) {
  this.chatService = chatService;
 }

 public Set 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 getUserNames() {
        Set users = new HashSet();
        users.add("Max");
        users.add("Scott");
        users.add("Bob");
        return users;
 }

}

 

 

struts.xml

        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

 
 
            /UserList.jsp
       

 

 

struts.properties

struts.objectFactory=spring

 

web.xml


 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">
 struts1
 
  struts-cleanup
  
   org.apache.struts2.dispatcher.ActionContextCleanUp
  

 

 
  struts2
  
   org.apache.struts2.dispatcher.FilterDispatcher
  

 

 
  struts-cleanup
  /*
 

 
  struts2
  /*
 

 
  velocity
  
   org.apache.velocity.tools.view.servlet.VelocityViewServlet
  

 

 
  velocity
  *.vm
 

 
  
   org.springframework.web.context.ContextLoaderListener
  

 

 
  index.html
  index.htm
  index.jsp
  default.html
  default.htm
  default.jsp
 


 

UserList.jsp

<%@ page contentType = "text/html; charset=UTF-8" %>
<%@ taglib prefix = "s" uri = "/struts-tags" %>


    User List


   

User List


   

       
           

  1.    

       


你可能感兴趣的:(j2ee)