此例子来自于《JavaEE企业应用实战》(李刚)
需要先导入spring和struts的jar包,一定要导入struts2-spring-plugin-版本号.jar,这是让spring和struts相关联的包。接下来是例子
struts.xml(在src文件下)
<?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.devMode" value="true"/>
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<package name="default" namespace="/" extends="struts-default">
<!-- 此处的class并不是一个类,而是一个bean的id -->
<action name="login" class="loginAction">
<result name="error">error.jsp</result>
<result>success.jsp</result>
</action>
<action name="*">
<result>{1}.jsp</result>
</action>
</package>
</struts>
web.xml(WEB-INF文件夹下)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>TestStruts5</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- ContextLoaderListener监听类实现了ServletContextListener接口,
它可以在创建时自动查找WEB-INF文件夹下的applicationContext.xml文件
如果要加载多个配置文件可以使用context-param对contextConfigLocation
的参数进行配置
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<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>
</web-app>
applicationContext.xml(在WEB-INF文件夹下)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="myService" class="com.service.impl.MyServiceImpl"/>
<bean id="loginAction" class="com.action.LoginAction" scope="prototype" p:myService-ref="myService"/>
</beans>
<font color="red">注意:</font>对Action配置一定要加scope属性,因为Action包含了请求的状态信息,必须为每个请求对应一个Action,所以不能将该Action实例配置成singleton行为
MyService接口
package com.service;
public interface MyService {
int validLogin(String username , String pass);
}
MyService接口实现类
package com.service.impl;
import com.service.MyService;
public class MyServiceImpl implements MyService {
@Override
public int validLogin(String username, String pass) {
// TODO Auto-generated method stub
if(username.equals(pass)) {
return 99;
}
return -1;
}
}
Action类
package com.action;
import com.opensymphony.xwork2.ActionSupport;
import com.service.MyService;
public class LoginAction extends ActionSupport {
private String username;
private String password;
private MyService myService;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setMyService(MyService myService) {
this.myService = myService;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
if(myService.validLogin(username, password) > 0) {
return SUCCESS;
}
return ERROR;
}
}
这里的myService只需要加set方法以便依赖注入。
接下来是测试用的页面
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:form action="login">
<s:textfield name="username" label="用户名"/>
<s:textfield name="password" label="密码"/>
<s:submit value="提交"/>
</s:form>
</body>
</html>
还需要一个success.jsp和error.jsp页面来代表action相应是成功还是失败的,我这里I就不写出来了。