注:spring的作用就是实例化bean,整合struts实际上就是使用spring管理action类的过程。
1、导入jar包
导入struts和spring的jar,struts还需要一个struts2-spring-plugin-2.3.4.1.jar。
2、web.xml
需要配置struts和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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>spring_struts2</display-name>
<!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 对Spring容器进行实例化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置sturts2 -->
<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>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3、struts.xml
action的class不是bean类本身,而是spring的bean的id名称
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="ru" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<action name="login" class="loginaction">
<result name="sucess">
/jsp/logsucess.jsp
</result>
<result name="error">
/jsp/logerror.jsp
</result>
</action>
</package>
</struts>
4、LoginAction.java(action类)
里面的对象属性由spring负责实例化。
package com.ru.action;
import com.opensymphony.xwork2.ActionSupport;
import com.ru.service.UserCheck;
public class LoginAction extends ActionSupport {
private String username;
private String password;
private UserCheck uc;
public void setUc(UserCheck uc) {
this.uc = uc;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String execute() throws Exception {
if(uc.result(username, password)=="sucess"){
return "sucess";
}else{
return "error";
}
}
}
5、UserCheck.java(这个是业务逻辑层的判断代码)
package com.ru.service;
public class UserCheck {
public String result(String username,String password){
if("ru".equals(username)&&"123".equals(password)){
return "sucess";
}else{
return "error";
}
}
}
6、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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- services -->
<!-- 配置一个业务逻辑bean -->
<bean id="usercheck" class="com.ru.service.UserCheck"/>
<!-- 配置一个action -->
<bean id="loginaction" class="com.ru.action.LoginAction">
<property name="uc" ref="usercheck"/>
</bean>
</beans>
7、login.jsp
注意的是:action实现的是重定向,说以需要完整访问路径,这里使用<base href="<%=basePath%>">,获取完整路径
- <%@pagelanguage="java"contentType="text/html;charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@tagliburi="/struts-tags"prefix="s"%>
- <%
- Stringpath=request.getContextPath();
- StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <basehref="<%=basePath%>">
- <metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
- <title>Inserttitlehere</title>
- </head>
- <body>
- <!--使用struts标签创建一个表单-->
- <s:formaction="login"method="post">
- <s:textfieldkey="用户名"name="username"></s:textfield>
- <s:passwordkey="密 码"name="password"></s:password>
- <s:submitvalue="登录"></s:submit>
- </s:form>
- </body>
- </html>