1.下载所需包 在struts2 官网下载相关包,插入到WebConetent/WEB-INF/lib下。struts2里的包比較多,能够先加入一些主要的包。再依据调试结果加入。我程序加入例如以下包就可以执行:

2.新建一个项目,按1加入包。

3.配置web.xml.主要是加入struts2过滤器

<?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" id="WebApp_ID" version="3.0">
  <display-name>FirstStruts2</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>
  
  <!--struts2 过滤器!  -->
  <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>
4.配置struts.xml(不是struts2.xml) 这里说下项目的功能。就是index.jsp中有个表单,假设输入为空则转到error.jsp。否则转到success.jsp

<?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>
	<!-- 声明包 -->
    <package name="cqu" extends="struts-default">
    	<!-- 定义action -->
    	<action name="greeting" class="cqu.GreetingAction">
    		<!-- 定义成功的映射页面 -->
    		<result name="success">success.jsp</result>
    		<!-- 定义失败的映射页面 -->
    		<result name="error">error.jsp</result>
    	</action>
    </package>
</struts>
注意上面的DOCTYPE声明中的dtds版本号(我的版本号是struts-2.3.16.3)

5.GreetingAction用来处理表单信息,推断是不是无效的

package cqu;

import com.opensymphony.xwork2.ActionSupport;

public class GreetingAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String username;
	public String execute() throws Exception{
		if(username==null||"".equals(username)){
			return ERROR;
		}else {
			return SUCCESS;
		}
	}
	public String getUsername(){
		return username;
	}
	public void setUsername(String username){
		this.username=username;
	}
}
6.index.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>
	<form action="greeting.action" method="post">
	please input your name:<input type="text" name="username">
	<input type="submit" value="submit">
	</form>
<body>

</body>
</html>
7.success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:property value="username"/><br>
welecome@!!!!!!!!
</body>
</html>

当中
<s:property value="username"/>
是获取提交表单中的数据。<s:/>是struts2的标签。

使用之间要插入该标签库。见第三行。

8.errror.jsp略

9.以下是整个项目文件夹。注意xml存放的位置!!

!!jsp文件放在WebContent下web.xml放在WEB-INF下,struts.xml放在src下而不是放在包内。