Struts2.1+Spring2.0+Hibernate3.1


 

项目配置图:

 Struts2.1 Jar包:

 其他必须包:
 

 
 

<提示:必须要导入struts2-spring-plugin-x.x.x.x.jar,不然无法托管struts的action于spring>

 Spring与Hibernate包的配置与文章《SSH1基本配置(Struts1.2 + Spring2.0 + Hibernate3.1)》一致
地址:http://sunspot.blog.51cto.com/372554/468430

 

web.xml

  1. <!-- Spring Configuration --> 
  2.     <context-param> 
  3.         <param-name>contextConfigLocation</param-name> 
  4.         <param-value>/WEB-INF/applicationContext.xml</param-value> 
  5.     </context-param> 
  6.  
  7.     <listener> 
  8.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
  9.     </listener> 
  10.  
  11.     <filter> 
  12.         <filter-name>CharacterEncoding</filter-name> 
  13.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
  14.         <init-param> 
  15.             <param-name>encoding</param-name> 
  16.             <param-value>gb2312</param-value> 
  17.         </init-param> 
  18.     </filter> 
  19.     <filter-mapping> 
  20.         <filter-name>CharacterEncoding</filter-name> 
  21.         <url-pattern>/*</url-pattern> 
  22.     </filter-mapping> 
  23.       
  24.     <!-- Struts2 Configuration --> 
  25.     <filter> 
  26.         <filter-name>struts2</filter-name> 
  27.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> 
  28.     </filter> 
  29.  
  30.     <filter-mapping> 
  31.         <filter-name>struts2</filter-name> 
  32.         <url-pattern>/*</url-pattern> 
  33.     </filter-mapping> 
  34.  
  35.     <!-- ONGL Configuration --> 
  36.     <filter> 
  37.         <filter-name>struts-cleanup</filter-name> 
  38.         <filter-class> 
  39.             org.apache.struts2.dispatcher.ActionContextCleanUp  
  40.         </filter-class> 
  41.     </filter> 
  42.  
  43.     <filter-mapping> 
  44.         <filter-name>struts-cleanup</filter-name> 
  45.         <url-pattern>/*</url-pattern> 
  46.     </filter-mapping> 

 

struts.xml

    这里的action处理类托管给Spring的loginAction

  1. <!-- Struts2 Action Configuration --> 
  2.     <package name="main" namespace="/" extends="struts-default"> 
  3.         <action name="login" class="loginAction" > 
  4.             <result name="success">/success.jsp</result> 
  5.             <result name="input">/input.jsp</result> 
  6.             <result name="error">/failure.jsp</result>   
  7.         </action> 
  8.     </package> 

applicationContext.xml

    这里的loginAction就是托管struts的action类,并且注入Service

  1. <!-- Spring Action Cofiguration --> 
  2.     <bean id="userService" class="com.ssh2.service.UserService" factory-method="getInstance" lazy-init="true"/> 
  3.     <bean name="loginAction" class="com.ssh2.beans.User" lazy-init="false" depends-on="userService"> 
  4.         <property name="service"><ref local="userService"/></property> 
  5.     </bean> 

User.java

    UserService与SSH1中的一致。 
            
http://sunspot.blog.51cto.com/372554/468430

  1. package com.ssh2.beans;  
  2.  
  3. import java.io.Serializable;  
  4. import com.opensymphony.xwork2.Action;  
  5. import com.opensymphony.xwork2.ActionContext;  
  6. import com.opensymphony.xwork2.ActionSupport;  
  7. import com.ssh2.service.UserService;  
  8.  
  9. public class User extends ActionSupport implements Serializable {  
  10.     private String id;  
  11.     private String password;  
  12.     private String name;  
  13.     private Integer age;  
  14.     public User() {  
  15.         super();  
  16.     }  
  17.     public String getId() {  
  18.         return id;  
  19.     }  
  20.     public void setId(String id) {  
  21.         this.id = id;  
  22.     }  
  23.     public String getName() {  
  24.         return name;  
  25.     }  
  26.     public void setName(String name) {  
  27.         this.name = name;  
  28.     }  
  29.     public Integer getAge() {  
  30.         return age;  
  31.     }  
  32.     public void setAge(Integer age) {  
  33.         this.age = age;  
  34.     }  
  35.     public String getPassword() {  
  36.         return password;  
  37.     }  
  38.     public void setPassword(String password) {  
  39.         this.password = password;  
  40.     }  
  41.  
  42.     private UserService service;  
  43.       
  44.     public UserService getService() {  
  45.         return service;  
  46.     }  
  47.  
  48.     public void setService(UserService service) {  
  49.         this.service = service;  
  50.     }  
  51.  
  52.     public String login() throws Exception {  
  53.         if(service.validate(this.getId(), this.getPassword())) {  
  54.             ActionContext.getContext().put("user", service.getUser(this.getId()));  
  55.             return Action.SUCCESS;  
  56.         } else {  
  57.             return Action.ERROR;  
  58.         }  
  59.     }  
  60. }  

 总结:
     重点在配置struts.xml中的action时的处理,可以使用spring的对象引用进行配置,但一定要保证对象类型一致。