Struts2 由 struts.xml xxx.java web.xml xxx.jsp 组成
执行的规则是:首先访问 xxx1.jsp ,显示 xx1.jsp 界面,一番操作后访问服务器的请求被 web.xml 拦截,然后对应的 struts.xml 中 action 被激活,action 中相应的类 xxx.java 执行,执行后放回一个 String 类型的值,然后继续查找 action 中 result 的 name 属性,执行相应 result 对应的 xxx2.jsp
项目图:
web.xml 是配置拦截器的。
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> 未指定打开界面时,系统自动选择的最开始的界面(这里是列表,可以有多项) <welcome-file>index.jsp</welcome-file> 欢迎界面,优先级从上到下 </welcome-file-list> <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>
xxx.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% 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>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="zhua"> 此处的action要对应Struts.xml中配置的action名称!!!!!!!!!!!!!! <ul style="list-style-type: none;"> <li>账号:<input type="text" name="zhanghao"> </li> <li>密码:<input type="text" name="mima"> </li> <li><input type="submit" value="提交"> </li> </ul> </form> </body> </html>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="sss" extends="struts-default"> 包名可以自己定,扩展 struts-default(默认的)(不知道原因) <action name="zhua" class="text.Zhua"> action!!名称为 zhua ,对应的类为 text.Zhua (action的名称应与 xxx.jsp中form标签的action一致) <result name="ok">/ok.jsp</result> 对应返回值,显示相应的界面 <result name="no">/no.jsp</result> </action> </package> </struts>
xxx.java
package text; import com.opensymphony.xwork2.ActionSupport; public class Zhua extends ActionSupport 继承类 { private String zhanghao; 定义账号与密码 private String mima; public String getZhanghao() { return zhanghao; } public void setZhanghao(String zhanghao) { this.zhanghao = zhanghao; } public String getMima() { return mima; } public void setMima(String mima) { this.mima = mima; } @Override public String execute() throws Exception 重要的方法 { System.out.println(zhanghao+mima); // TODO Auto-generated method stub if(zhanghao.equals("chen")) { if(mima.equals("chang")) { return "ok"; 返回值ok } } return "xx"; 返回值no } }
ok.jsp 跳转的界面 ,基本不用改动
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% 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>My JSP 'ok.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> ok <br> 唯一更改的地方 </body> </html>