IDE是Myeclipse6.0
步骤如下:
步骤一: 先配置Struts2与Spring的环境:
导入Struts2需要的5个包(commons-logging-1.0.4.jar; freemarker-2.3.8.jar; ognl-2.6.11.jar; struts2-core-2.0.14.jar; xwork-2.0.7.jar),再导入Spring需要的3个包(spring.jar; commons-logging.jar; struts2-spring-plugin-2.0.14.jar);
步骤二:开发两个JSP页面
login.jsp与success.jsp,代码分别是:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ 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>
<title>My JSP 'login.jsp' starting page</title>
</head>
<body>
<s:form action="login" method="post">
<s:textfield name="username" label="username" ></s:textfield>
<s:textfield name="password" label="password"></s:textfield>
<s:submit label="submit"></s:submit>
</s:form>
</body>
</html>
与
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>
username:${requestScope.username}<br>
password:${requestScope.password}
</body>
</html>
步骤三:开发一个Action,名为LoginAction,文件(LoginAction.java)如下:
package com.ycw.strutsspring;
import com.opensymphony.xwork2.ActionSupport;
import com.ycw.strutsspring2.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 MyService getMyService() {
return myService;
}
public void setMyService(MyService myService) {
this.myService = myService;
}
public String execute() throws Exception {
if (myService.valid(username, password)) {
return SUCCESS;
}
return INPUT;
}
}
步骤四:开发一个业务逻辑类(带接口):
接口类为:MyService.java
package com.ycw.strutsspring2;
public interface MyService {
boolean valid(String username,String password);
}
实现类为:MyServiceImpl.java
package com.ycw.strutsspring2;
public class MyServiceImpl implements MyService {
public boolean valid(String username, String password) {
if(username.equals("hello") && password.equals("world")) {
return true;
}
return false;
}
}
步骤五:配置Web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.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>
</web-app>
步骤六:配置applicationContext.xml(注意:这个文件要放在WEB-INF下与Web.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="mys" class="com.ycw.strutsspring2.MyServiceImpl"></bean>
<bean id="loginAction" class="com.ycw.strutsspring.LoginAction" scope="prototype">
<property name="myService" ref="mys"/>
</bean>
</beans>
步骤七:配置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="struts2" extends="struts-default">
<action name="login" class="loginAction">
<result name="success">/success.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
步骤八:访问jsp页面并作相应的操作即可