天天看点

Struts Spring Hibernate 整合:SSH2

Struts2.1+Spring2.0+Hibernate3.1

项目配置图:

<a target="_blank" href="http://blog.51cto.com/attachment/201012/144357212.jpg"></a>

 Struts2.1 Jar包:

<a target="_blank" href="http://blog.51cto.com/attachment/201012/144615926.jpg"></a>

 其他必须包:

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

 Spring与Hibernate包的配置与文章《SSH1基本配置(Struts1.2 + Spring2.0 + Hibernate3.1)》一致

web.xml

&lt;!-- Spring Configuration --&gt; 

    &lt;context-param&gt; 

        &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; 

        &lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt; 

    &lt;/context-param&gt; 

    &lt;listener&gt; 

        &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; 

    &lt;/listener&gt; 

    &lt;filter&gt; 

        &lt;filter-name&gt;CharacterEncoding&lt;/filter-name&gt; 

        &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt; 

        &lt;init-param&gt; 

            &lt;param-name&gt;encoding&lt;/param-name&gt; 

            &lt;param-value&gt;gb2312&lt;/param-value&gt; 

        &lt;/init-param&gt; 

    &lt;/filter&gt; 

    &lt;filter-mapping&gt; 

        &lt;url-pattern&gt;/*&lt;/url-pattern&gt; 

    &lt;/filter-mapping&gt; 

    &lt;!-- Struts2 Configuration --&gt; 

        &lt;filter-name&gt;struts2&lt;/filter-name&gt; 

        &lt;filter-class&gt;org.apache.struts2.dispatcher.FilterDispatcher&lt;/filter-class&gt; 

    &lt;!-- ONGL Configuration --&gt; 

        &lt;filter-name&gt;struts-cleanup&lt;/filter-name&gt; 

        &lt;filter-class&gt; 

            org.apache.struts2.dispatcher.ActionContextCleanUp  

        &lt;/filter-class&gt; 

struts.xml

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

&lt;!-- Struts2 Action Configuration --&gt; 

    &lt;package name="main" namespace="/" extends="struts-default"&gt; 

        &lt;action name="login" class="loginAction" &gt; 

            &lt;result name="success"&gt;/success.jsp&lt;/result&gt; 

            &lt;result name="input"&gt;/input.jsp&lt;/result&gt; 

            &lt;result name="error"&gt;/failure.jsp&lt;/result&gt;   

        &lt;/action&gt; 

    &lt;/package&gt; 

applicationContext.xml

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

&lt;!-- Spring Action Cofiguration --&gt; 

    &lt;bean id="userService" class="com.ssh2.service.UserService" factory-method="getInstance" lazy-init="true"/&gt; 

    &lt;bean name="loginAction" class="com.ssh2.beans.User" lazy-init="false" depends-on="userService"&gt; 

        &lt;property name="service"&gt;&lt;ref local="userService"/&gt;&lt;/property&gt; 

    &lt;/bean&gt; 

User.java

    UserService与SSH1中的一致。 

package com.ssh2.beans;  

import java.io.Serializable;  

import com.opensymphony.xwork2.Action;  

import com.opensymphony.xwork2.ActionContext;  

import com.opensymphony.xwork2.ActionSupport;  

import com.ssh2.service.UserService;  

public class User extends ActionSupport 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;  

    private UserService service;  

    public UserService getService() {  

        return service;  

    public void setService(UserService service) {  

        this.service = service;  

    public String login() throws Exception {  

        if(service.validate(this.getId(), this.getPassword())) {  

            ActionContext.getContext().put("user", service.getUser(this.getId()));  

            return Action.SUCCESS;  

        } else {  

            return Action.ERROR;  

        }  

}  

 总结:

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

本文转自 sundunjam 51CTO博客,原文链接:http://blog.51cto.com/sunspot/469002,如需转载请自行联系原作者