Struts Spring Hibernate 整合:SSH1

Struts1.2 + Spring2.0 + Hibernate3.1

 图示:

 Struts1.2 Jar包:
 

 Spring2.0 Jar包:

 Hibernate3.1 Jar包:
     官方下载的Hibernate3.1文件夹lib下的全部Jar包

 其他必须包:

web.xml 配置:

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

 

struts-config.xml

    这里的Action使用DelegatingActionProxy进行托管,所有Action应该托管此类

  
  
  
  
  1. <!-- Bean Configuration --> 
  2. <form-beans> 
  3.         <form-bean name="user" type="com.ssh1.beans.User"></form-bean> 
  4.     </form-beans> 
  5.  
  6. <!-- Action Configuration --> 
  7.     <action-mappings> 
  8.         <action name="user" path="/login" input="/input.jsp" scope="request" 
  9.             type="org.springframework.web.struts.DelegatingActionProxy"> 
  10.             <forward name="success" path="/success.jsp"></forward> 
  11.             <forward name="failure" path="/failure.jsp"></forward> 
  12.         </action> 
  13.     </action-mappings> 

 

applicationContext.xml

   这里需要注意的是,applicationContext中的Action配置的name要与struts-config.xml中Action的path值相同

  
  
  
  
  1. <!-- Service Configuration -->
  2. <bean id="userService" class="com.ssh1.service.UserService" factory-method="getInstance" lazy-init="true"/> 
  3.     
  4. <!-- Action Configuration -->
  5. <bean name="/login" class="com.ssh1.actions.LoginAction" lazy-init="true" depends-on="userService"> 
  6.         <property name="service"><ref local="userService"/></property> 
  7.     </bean> 

 

User.java

  
  
  
  
  1. package com.ssh1.beans;  
  2. import org.apache.struts.action.ActionForm;  
  3. public class User extends ActionForm implements Serializable {  
  4.     private String id;  
  5.     private String password;  
  6.     private String name;  
  7.     private Integer age;  
  8.       
  9.     public User() {  
  10.         super();  
  11.     }  
  12.  
  13.     public String getId() {  
  14.         return id;  
  15.     }  
  16.     public void setId(String id) {  
  17.         this.id = id;  
  18.     }  
  19.     public String getName() {  
  20.         return name;  
  21.     }  
  22.     public void setName(String name) {  
  23.         this.name = name;  
  24.     }  
  25.     public Integer getAge() {  
  26.         return age;  
  27.     }  
  28.     public void setAge(Integer age) {  
  29.         this.age = age;  
  30.     }  
  31.     public String getPassword() {  
  32.         return password;  
  33.     }  
  34.     public void setPassword(String password) {  
  35.         this.password = password;  
  36.     }     

LoginAction.java

  
  
  
  
  1. package com.ssh1.actions;  
  2.  
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.servlet.http.HttpServletResponse;  
  5.  
  6. import org.apache.struts.action.Action;  
  7. import org.apache.struts.action.ActionForm;  
  8. import org.apache.struts.action.ActionForward;  
  9. import org.apache.struts.action.ActionMapping;  
  10.  
  11. import com.ssh1.beans.User;  
  12. import com.ssh1.service.UserService;  
  13.  
  14. public class LoginAction extends Action {  
  15.     private UserService service;  
  16.  
  17.     public UserService getService() {  
  18.         return service;  
  19.     }  
  20.  
  21.     public void setService(UserService service) {  
  22.         this.service = service;  
  23.     }  
  24.  
  25.     @Override 
  26.     public ActionForward execute(ActionMapping mapping, ActionForm form,  
  27.             HttpServletRequest request, HttpServletResponse response)  
  28.             throws Exception {  
  29.         User user = (User) form;  
  30.         if(service.validate(user.getId(), user.getPassword())) {  
  31.             request.getSession().setAttribute("user", service.getUser(user.getId()));  
  32.             return mapping.findForward("success");  
  33.         } else {  
  34.             return mapping.findForward("failure");  
  35.         }  
  36.     }  

 

UserService.java

  
  
  
  
  1. package com.ssh1.service;  
  2.  
  3. import com.ssh1.beans.User;  
  4. import com.ssh1.dao.UserDAO;  
  5.  
  6. public class UserService {  
  7.     private static UserService us;  
  8.     private UserDAO dao;
  9.  
  10. //为了减少对象之间的耦合,也可以将DAO在applicationContext中配置,
  11. //之后注入UserService当中。  
  12.     private UserService() {  
  13.         dao = new UserDAO();  
  14.     }  
  15.     public static UserService getInstance() {  
  16.         if(us == null) {  
  17.             us = new UserService();  
  18.         }  
  19.         return us;  
  20.     }  
  21.       
  22.     public boolean validate(String id, String password) {  
  23.         return dao.validate(id, password);  
  24.     }  
  25.       
  26.     public User getUser(String id) {  
  27.         return (User) dao.get(id);  
  28.     }  
  29. }  

UserDAO.java

 参照UserService.java中的方法,只要实现该接口即可。
    代码略

 总结:
    重点在于配置struts-config.xml中的action时,要使用DelegationActionProxy托管,并且struts-config.xml中action的path值要与applicationContext的托管action的name值相同。

你可能感兴趣的:(Hibernate,职场,整合,休闲,SSH1)