1、 所用的环境struts2-2.2.1,eclipse-jee-helios-SR1-win32,jdk-6u21-windows-i586,apache-tomcat-7.0.2。
2、 新建一个动态javaweb项目。
3、 将commons-logging,freemarker,ognl,struts2-core,xwork,fileupload,common-io这七个基础jar包放在WebContent/WEB-INF/lib下。
4、 新建web.xml 文件,放在WebContent/WEB-INF目录下。其内容如下:
<?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">
<display-name>Struts Blank</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>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
5、 新建struts.xml文件, 放在src文件夹下。其内容如下:
<?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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<package name="main" namespace="/" extends="struts-default">
<action name="test" class="com.demo.struts2.actions.TestAction">
<result name="success">success.jsp</result>
<result name="input">input.jsp</result>
</action>
</package>
<!-- Add packages here -->
</struts>
6、 新建类TestAction
package com.demo.struts2.actions;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport {
protected String str1 = null;
protected String str2 = null;
public String getStr1() {
return str1;
}
public void setStr1(String str1) {
this.str1 = str1;
}
public String getStr2() {
return str2;
}
public void setStr2(String str2) {
this.str2 = str2;
}
public String execute() throws Exception{
if(!str1.equals("") && !str2.equals(""))
{
ActionContext.getContext().getSession().put("str1", str1);
ActionContext.getContext().getSession().put("str2", str2);
return Action.SUCCESS;
}else{
return Action.INPUT;
}
}
}
7、 新建input.jsp 文件和 success.jsp文件,都放在WebContent目录下。
input.jsp文件内容如下:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="test.action" method="post">
<input type="text" name="str1">
<input type="text" name="str2">
<input type="submit" value="submit"></form>
</body>
</html>
注意:form表单中的action=”test.action”也可写成action=”test”.如果没用进行另外配置的话,写成action=”test.do”是会出错的。
success.jsp文件内容如下:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
str1=<%=request.getAttribute("str1") %>
str2=<%=request.getAttribute("str2") %>
</body>
</html>
8、 发布到tomcat7上,在浏览器上输入地址(这里struts2demo是项目名称)
http://localhost:8080/struts2demo/input.jsp
出现如下输入界面。
输入信息后点击submit按钮,跳转到信息显示界面。