参考:http://lukuijun.iteye.com/blog/350750
以下为我自己整理的步骤:
Struts2和Spring整合,创建一个web项目TestSS
1、整合struts2
1)导入strut2的jar包:
commons-logging-1.0.4.jar,
freemarker-2.3.8.jar,
ognl-2.6.11.jar,
struts2-core-2.0.12.jar,
xwork-2.06.jar,
javassist-3.11.0.GA.jar,
commons-fileupload-1.2.2.jar
commons-lang-2.5.jar。暂时导入这些jar包,到时候需要再导入。
2)将struts.xml文件放置在TestSS工程的src目录下。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.objectFactory.spring.autoWire" value="name"></constant>
<package name="struts2" extends="struts-default" >
<action name="login" class="com.test.action.LoginAction">
<result name="input">/login.jsp</result>
<result >/success.jsp</result>
</action>
</package>
</struts>
3)在web.xml里面配置struts2用到的核心过滤器。
<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>
2、整合spring
1)导入spring的jar包:
spring.jar
struts2-core-2.2.3.1.jar
spring-web-2.5.6.jar
spring-core-2.5.6.jar
spring-context-2.5.6.jar
spring-beans-2.5.6.jar
2)将applicationContext.xml文件放置在TestStrut2工程的WEB-INF目录下。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byName">
<!-- 构造注入 -->
<!--
<bean id="loginDao" class="com.test.dao.impl.LoginDaoImpl"></bean>
<bean id="loginService" class="com.test.service.impl.LoginServiceImpl">
<property name="loginDao" ref="loginDao"></property>
</bean>
<bean id="loginAction" class="com.test.action.LoginAction" scope="prototype">
<property name="loginService" ref="loginService"></property>
</bean>
-->
<!-- 改成自动装配如下 default-autowire="byName" -->
<bean id="loginDao" class="com.test.dao.impl.LoginDaoImpl"/>
<bean id="loginService" class="com.test.service.impl.LoginServiceImpl"/>
</beans>
3)在web.xml里面配置spring用到的监听器。
<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>
4)添加struts2-spring整合的插件:struts2-spring-plugin-2.2.3.1.jar,如果不使用这个插件,则需要在struts.xml里面配置:
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
5)可以在web-inf下加入log4j的lib,在web.xml配置如下:
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>TestSS.root</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
3、测试struts2和spring整合对不对? 以下代码为项目配置完成后所有代码。
代码如下:
applicationContest.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byName"> <!-- 构造注入 --> <!-- <bean id="loginDao" class="com.test.dao.impl.LoginDaoImpl"></bean> <bean id="loginService" class="com.test.service.impl.LoginServiceImpl"> <property name="loginDao" ref="loginDao"></property> </bean> <bean id="loginAction" class="com.test.action.LoginAction" scope="prototype"> <property name="loginService" ref="loginService"></property> </bean> --> <!-- 改成自动装配如下 default-autowire="byName" --> <bean id="loginDao" class="com.test.dao.impl.LoginDaoImpl"/> <bean id="loginService" class="com.test.service.impl.LoginServiceImpl"/> </beans>
log4j中xml的配置
# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!
# For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.
log4j.rootLogger=INFO, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${TestSS.root}/WEB-INF/petstore.log
log4j.appender.logfile.MaxFileSize=512KB
# Keep three backup files.
log4j.appender.logfile.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Login</title>
</head>
<body>
<s:form action="login.action">
<s:textfield label="用户" name="user.name"></s:textfield>
<s:textfield label="密码" name="user.pwd"></s:textfield>
<s:submit label="登录"></s:submit>
</s:form>
</body>
</html>
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>SUCCESS</title>
</head>
<body>
<s:property value="user.name"/>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>
<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>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>TestSS.root</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default1" extends="struts-default">
<action name="login" class="com.test.action.LoginAction" >
<result>success.jsp</result>
</action>
</package>
</struts>
User类
package com.test.vo;
public class User {
private String name;
private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
LoginAction
package com.test.action;
import com.opensymphony.xwork2.Action;
import com.test.service.LoginService;
import com.test.vo.User;
public class LoginAction implements Action {
private LoginService loginService;
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String execute() throws Exception {
System.out.println("user----");
if(loginService.isValid()){
System.out.println("is valid user");
}
return SUCCESS;
}
public LoginService getLoginService() {
return loginService;
}
public void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
}
service接口
package com.test.service;
public interface LoginService {
boolean isValid();
}
service实现类
package com.test.service.impl;
import com.test.dao.LoginDao;
import com.test.service.LoginService;
public class LoginServiceImpl implements LoginService {
LoginDao loginDao ;
public boolean isValid() {
return loginDao.isValid();
}
public LoginDao getLoginDao() {
return loginDao;
}
public void setLoginDao(LoginDao loginDao) {
this.loginDao = loginDao;
}
}
dao接口
package com.test.dao;
public interface LoginDao {
boolean isValid();
}
dao实现类
package com.test.dao.impl;
import com.test.dao.LoginDao;
public class LoginDaoImpl implements LoginDao{
public boolean isValid() {
return true;
}
}
运行tomcat启动:http://localhost:8080/TestSS/login.jsp
输入用户名和密码,点提交后后台打印出
user----
is valid user