1.Struts2与Spring的集成相当于将Action的实例化交给Spring来管理
在src目录下新建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>
<constant name="struts.objectFactory" value="spring" />
<!--value="spring"将Action的实例交给Spring -->
<package name="google" extends="struts-default">
<action name="login" class="loginAction">
<result name="success">/WEB-INF/page/success.jsp</result>
<result name="error">/WEB-INF/page/error.jsp</result>
</action>
</package>
<!-- Add packages here -->
</struts>
<!-- class的只对应Spring中id值-->
2.在WEB-INF下新建applicationContext.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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="loginService" class="com.struts2.service.impl.LoginServiceImpl"></bean>
<bean id="loginAction" class="com.struts2.action.LoginAction" scope="prototype">
<property name="loginService" ref="loginService"></property>
</bean>
</beans>
3.新建业务逻辑类和接口
package com.struts2.service;
public interface LoginService {
boolean isLogin(String userName,String password);
}
实现类
package com.struts2.service.impl;
import com.struts2.service.LoginService;
public class LoginServiceImpl implements LoginService {
public boolean isLogin(String userName, String password) {
if("admin".equals(userName) && "123456".equals(password))
return true;
else
return false;
}
}
4.新建Action类
package com.struts2.action;
import com.struts2.service.LoginService;
public class LoginAction {
private LoginService loginService;
private String userName;
private String passWord;
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String execute()throws Exception{
if(loginService.isLogin(userName, passWord)){
return "success";
}else
return "error";
}
public LoginService getLoginService() {
return loginService;
}
public void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
}
5.最后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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Struts通过Filter启动,Spring通过Listener启动
6.客户端脚本
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="login" method="post">
用户名:<input type="text" name="userName"/><br>
密码:<input type="password" name="passWord"/><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>