开发一个Spring与Struts 2的整合项目的步骤如下。
1 创建Web项目Struts_Spring
2 添加Struts 2框架
配置web.xml,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>Struts_Spring</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
</web-app>
3 创建login.jsp
login.jsp代码如下:
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>登录界面</title>
</head>
<body>
<s:form action="login.action" method="post">
<s:textfield name="xh" label="学号"/>
<s:password name="kl" label="口令"/>
<s:submit value="登录"/>
</s:form>
</body>
</html>
4 创建Action
LoginAction.java代码如下:
package org.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private String xh;
private String kl;
public String getXh() {
return xh;
}
public void setXh(String xh) {
this.xh = xh;
}
public String getKl() {
return kl;
}
public void setKl(String kl) {
this.kl = kl;
}
public String execute() throws Exception {
return SUCCESS;
}
}
配置struts.xml文件,代码如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<include file="struts-default.xml"/>
<package name="default" extends="struts-default" namespace="/">
<action name="login" class="org.action.LoginAction">
<result name="success">/login_success.jsp</result>
</action>
</package>
</struts>
5 创建login_success.jsp
代码如下:
<%@ page contentType="text/html;charset=utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body >
<h2>您好! <s:property value=" xh"/>欢迎您登录成功 </h2>
</body>
</html>
6 部署运行
部署,测试Struts 2是否正常。运行http://localhost:8080/Struts_Spring/login.jsp,在登录框和密码框中任意输入,单击【登录】按钮,转向登录成功界面,并输出登录名。
7 添加Spring框架
步骤同5.2.2节。
8 添加Spring支持包struts2-spring-plugin.jar
注意,一定要加入该包,该包位于Struts 2的lib目录下。
9 修改web.xml内容
修改web.xml内容,使得程序增加对Spring的支持。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>Struts_Spring</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>
10 创建消息包文件struts.properties
在src文件夹下创建struts.properties文件,使得Struts 2的类的生成交给Spring完成。步骤如下:右击项目的src文件夹,在弹出的菜单中选择【New】→【File】菜单项,之后在Enter or select the parent folder中输入struts2_spring/src,在File name栏中写入struts.properties,单击【确定】按钮。文件内容如下:
struts.objectFactory=spring
11 修改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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="loginAction" class="org.action.LoginAction"></bean>
</beans>
12 修改struts.xml
使得struts 2的类的生成交给Spring完成。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<include file="struts-default.xml"/>
<package name="default" extends="struts-default" namespace="/">
<action name="login" class="loginAction">
<result name="success">/login_success.jsp</result>
</action>
</package>
</struts>