您的位置:首页 > 编程语言 > Java开发

Struts Spring Hibernate 整合:SSH2

2010-12-30 15:30 453 查看
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)》一致

地址:/article/4127867.html

web.xml

<!-- Spring 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>

<!-- Struts2 Configuration -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- ONGL Configuration -->
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


struts.xml

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

<!-- Struts2 Action Configuration -->
<package name="main" namespace="/" extends="struts-default">
<action name="login" class="loginAction" >
<result name="success">/success.jsp</result>
<result name="input">/input.jsp</result>
<result name="error">/failure.jsp</result>
</action>
</package>

applicationContext.xml

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

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

User.java

UserService与SSH1中的一致。

/article/4127867.html

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的对象引用进行配置,但一定要保证对象类型一致。
本文出自 “博远至静” 博客,请务必保留此出处http://sunspot.blog.51cto.com/372554/469002
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: