程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Struts Spring Hibernate整合:SSH1

Struts Spring Hibernate整合:SSH1

編輯:關於JAVA

Struts1.2 + Spring2.0 + Hibernate3.1

圖示:

Struts1.2 Jar包:

Spring2.0 Jar包:

Hibernate3.1 Jar包:

官方下載的 Hibernate3.1文件夾lib下的全部Jar包

其他必須包:

web.xml 配置:

<!-- Struts 1 Configuration -->
<servlet> 
        <servlet-name>action</servlet-name> 
        <servlet-class> 
            org.apache.struts.action.ActionServlet  
        </servlet-class> 
        <init-param> 
            <param-name>config</param-name> 
            <param-value>/WEB-INF/struts-config.xml</param-value> 
        </init-param> 
        <init-param> 
            <param-name>debug</param-name> 
            <param-value>2</param-value> 
        </init-param> 
        <init-param> 
            <param-name>detail</param-name> 
            <param-value>2</param-value> 
        </init-param> 
        <load-on-startup>2</load-on-startup> 
    </servlet> 
     
    <servlet-mapping> 
        <servlet-name>action</servlet-name> 
        <url-pattern>*.do</url-pattern> 
    </servlet-mapping> 
<!-- Spring 2 Configuration -->
<context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </context-param> 
     
    <listener> 
        <listener-

class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
     
    <filter> 
        <filter-name>CharacterEncoding</filter-name> 
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-

class> 
        <init-param> 
            <param-name>encoding</param-name> 
            <param-value>gb2312</param-value> 
        </init-param> 
    </filter> 
    <filter-mapping> 
        <filter-name>CharacterEncoding</filter-name> 
        <url-pattern>/*</url-pattern> 
    </filter-mapping>

struts-config.xml

 這裡的Action使用 DelegatingActionProxy進行托管,所有Action應該托管此類

<!-- Bean Configuration -->
<form-beans> 
        <form-bean name="user" type="com.ssh1.beans.User"></form-bean> 
    </form-beans> 
     
<!-- Action Configuration -->
    <action-mappings> 
        <action name="user" path="/login" input="/input.jsp" scope="request"
            type="org.springframework.web.struts.DelegatingActionProxy"> 
            <forward name="success" path="/success.jsp"></forward> 
            <forward name="failure" path="/failure.jsp"></forward> 
        </action> 
    </action-mappings>

applicationContext.xml

這裡需要注意的是, applicationContext中的Action配置的name要與struts-config.xml中Action的path值相同

<!-- 

Service Configuration -->
<bean id="userService" class="com.ssh1.service.UserService" factory-method="getInstance" 

lazy-init="true"/> 
        
<!-- Action Configuration -->
<bean name="/login" class="com.ssh1.actions.LoginAction" lazy-init="true" depends-

on="userService"> 
        <property name="service"><ref local="userService"/></property> 
    </bean>

User.java

package com.ssh1.beans;  
import org.apache.struts.action.ActionForm;  
public class User extends ActionForm implements Serializable {  
    private String id;  
    private String password;  
    private String name;  
    private Integer age;  
          
    public User() {  
        super();  
    }  
     
    public String getId() {  
        return id;  
    }  
    public void setId(String id) {  
        this.id = id;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public Integer getAge() {  
        return age;  
    }  
    public void setAge(Integer age) {  
        this.age = age;  
    }  
    public String getPassword() {  
        return password;  
    }  
    public void setPassword(String password) {  
        this.password = password;  
    }     
}

LoginAction.java

package com.ssh1.actions;  
     
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
     
import org.apache.struts.action.Action;  
import org.apache.struts.action.ActionForm;  
import org.apache.struts.action.ActionForward;  
import org.apache.struts.action.ActionMapping;  
     
import com.ssh1.beans.User;  
import com.ssh1.service.UserService;  
     
public class LoginAction extends Action {  
    private UserService service;  
     
    public UserService getService() {  
        return service;  
    }  
     
    public void setService(UserService service) {  
        this.service = service;  
    }  
     
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,  
            HttpServletRequest request, HttpServletResponse response)  
            throws Exception {  
        User user = (User) form;  
        if(service.validate(user.getId(), user.getPassword())) {  
            request.getSession().setAttribute("user", service.getUser(user.getId()));  
            return mapping.findForward("success");  
        } else {  
            return mapping.findForward("failure");  
        }  
    }  
}

UserService.java

package com.ssh1.service;  
     
import com.ssh1.beans.User;  
import com.ssh1.dao.UserDAO;  
     
public class UserService {  
    private static UserService us;  
    private UserDAO dao;
     
//為了減少對象之間的耦合,也可以將DAO在applicationContext中配置,
//之後注入UserService當中。  
    private UserService() {  
        dao = new UserDAO();  
    }  
    public static UserService getInstance() {  
        if(us == null) {  
            us = new UserService();  
        }  
        return us;  
    }  
          
    public boolean validate(String id, String password) {  
        return dao.validate(id, password);  
    }  
          
    public User getUser(String id) {  
        return (User) dao.get(id);  
    }  
}

UserDAO.java

參照UserService.java中的方法,只要實現該接口即可。

代碼略

總結:

重點在於配置struts-config.xml中的action時,要使用DelegationActionProxy托管,並且struts -config.xml中action的path值要與applicationContext的托管action的name值相同。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved