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(不是struts.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)
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下而不是放在包内。
原文地址:http://blog.csdn.net/yang362046076/article/details/38225787