Struts2.1+Spring2.0+Hibernate3.1
其他必须包: <提示:必须要导入struts2-spring-plugin-x.x.x.x.jar,不然无法托管struts的action于spring> |
Spring与Hibernate包的配置与文章《SSH1基本配置(Struts1.2 + Spring2.0 + Hibernate3.1)》一致地址: |
web.xml
-
- <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>
-
-
- <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>
-
-
- <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
-
- <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
-
- <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中的一致。
- 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,如需转载请自行联系原作者