Struts Spring Hibernate 整合:SSH2
2010-12-30 15:30
453 查看
Struts2.1+Spring2.0+Hibernate3.1
web.xml
struts.xml
这里的action处理类托管给Spring的loginAction
applicationContext.xml
这里的loginAction就是托管struts的action类,并且注入Service
User.java
UserService与SSH1中的一致。
/article/4127867.html
本文出自 “博远至静” 博客,请务必保留此出处http://sunspot.blog.51cto.com/372554/469002
项目配置图: |
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 |
<!-- 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的对象引用进行配置,但一定要保证对象类型一致。 |
相关文章推荐
- SSH2整合 struts2.3.4.1 hibernate-annotations-3.4.0.GA spring2.5.6
- ssh2项目整合 struts2.1+hibernate3.3+spring3 基于hibernate注解和struts2注解
- Myeclipse10整合ssh2(struts2.1+spring3.1+hibernate4.1)+零注解零配置
- ssh2项目整合 struts2.1+hibernate3.3+spring3 基于hibernate注解和struts2注解
- SSH2整合时struts路径导致的异常
- Struts2.1.6+Spring2.5.6+Hibernate3.3.2+mysql整合+分页模板
- SSH开发实践part4:Spring整合Struts
- Struts文件上传整合
- struts整合spring
- ssh 整合之 struts1.x 和 spring2.5的整合
- spring+hibernate+struts整合(2)
- Android与服务器端数据交互(http协议整合struts2+android)
- jbpm4整合struts2+spring2.5+hibernate3.3入门实例教程
- 整合 Struts 和 Spring
- struts2+Hibernate4+spring3+EasyUI环境搭建之四:引入hibernate4以及spring3与hibernate4整合
- 就有关json与struts整合时过滤字段的笔记
- Struts和Spring整合的两种方式
- 基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (1)
- SSH2(Struts2.1+hibernate4.1+spring4.1)
- Spring2.5_Hibernate3.3_Struts2.16的整合思考,思路!