Struts2基础-2 -实现Action接口创建Action控制器
1.新建一个web项目,目录结构如下,添加jar包到lib文件夹里,并把jar包add 到 buildpath里面
2.web.xml配置 struts2的过滤器类:StrutsPrepareAndExecuteFilter ,把全部请求定位到该Struts2过滤器中
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 5 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 6 <welcome-file-list> 7 <welcome-file>index.jsp</welcome-file> 8 </welcome-file-list> 9 10 <filter> 11 <filter-name>StrutsPrepareAndExecuteFilter</filter-name> 12 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 13 </filter> 14 15 <filter-mapping> 16 <filter-name>StrutsPrepareAndExecuteFilter</filter-name> 17 <url-pattern>/*</url-pattern> 18 </filter-mapping> 19 20 </web-app>
3. 通过实现Action接口的方式来创建一个处理请求的action类:
(1)Action接口中定义了public String execute()方法,来执行用户请求的处理,此外,该没看还定义了五个字符类型的静态常量,是Action默认支持的逻辑视图名
常量 | 值 | 逻辑含义 |
SUCCESS | “success” | 表示程序处理正常,并返回给用户成功后的结果 |
NONE | "none" | 表示处理正常结束,但不返回给用户任何提示 |
ERROR | "error" | 表示处理结果失败 |
INPUT | "input" | 表示需要更多用户输入才能顺利执行 |
LOGIN | "login" | 表示需要用户正确登陆后才能顺利执行 |
(2)Action类中的属性可以直接来接收用户的输入(也就是获取请求参数),单表单提交爱哦时,Struts2自动对请求参数进行转换并对具有相同名字的Action属性进行赋值(通过setter方法,因此Action类中接收请求参数的属性,必须要设置getter setter方法)
(3)Action类的属性除了用来接收请求的数据,还可以用来输出处理结果,仍然是借助get set方法对结果属性进行处理,如下例子中所示。
(4)Struts2中,系统不会识别哪些舒心用于接收请求参数,哪些属性用于输出处理结果,只要对属性设置了get set方法,该属性就可以被自动处理。
(5)Action类中还可以使用复杂的属性,如用户自定义类,数组,集合对象等
1 package cn.test.action; 2 3 import com.opensymphony.xwork2.Action; 4 5 public class HelloAction implements Action { 6 7 private String name=""; //Action类中定义的属性设置的set get方法,就可以接收前端传递过来的与该属性名同名的参数值 8 private Integer age=0; 9 private String msg; 10 11 public String getName() { 12 return name; 13 } 14 15 public void setName(String name) { 16 this.name = name; 17 } 18 19 public Integer getAge() { 20 return age; 21 } 22 23 public void setAge(Integer age) { 24 this.age = age; 25 } 26 27 28 public String getMsg() { 29 return msg; 30 } 31 32 public void setMsg(String msg) { 33 this.msg = msg; 34 } 35 36 @Override 37 public String execute() throws Exception { 38 System.out.println("hello action"); 39 System.out.println("name value is:"+this.name); 40 System.out.println("age value is:"+this.getAge()); 41 if(this.getName().equals("admin")){ 42 this.setMsg("this is message from HelloAction"); //设置返回给页面的消息 43 return SUCCESS; 44 } else { 45 return ERROR; 46 } 47 } 48 }
4.编写Struts2的配置文件
(1)package元素定义Struts2处理请求的逻辑单元,其name属性用来指定包的名称(必须指定,并且唯一,因为要用于被其他包引用),extends用于指定要扩展的包
(2)action元素用于配置Action类,用于把一个请求的URI对应到一个Action类,name属性表示action的名称,必须指定,class属性可选,用于设定action类的全限定名
(3)result元素用于指定逻辑视图名,name属性表示result的逻辑视图名称(就是上边HelloAction类的execute方法的返回值),必须与Action类返回的字符串匹配;result元素的值表示与逻辑视图名对应的物理资源之间的映射,用来指定这个结果对应的实际资源的位置 注意,<result name="success">/success.jsp</result> 中,有/表示使用的绝对路径,是当前web应用程序的上下文路径,<result name="success">success.jsp</result> 无/ 相当于当前执行的Action路径
(4)result元素如果没写name属性,默认是返回success对应的逻辑视图
(5)package中的namespace的值+action元素中name属性的值,确定了一个唯一的请求action的路径
(6)以下面这个action为例,如果action里面没有指定method方法,那么调用默认的execute方法,企业项目中,还是要在result元素里面加上method属性,指定处理请求的方法。
<action name="helloStruts" class="cn.test.action.HelloAction"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 <!-- 开启动态调用,并且设置启用开发者模式 --> 7 <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> 8 <constant name="struts.devMode" value="true"></constant> 9 10 <package name="default" namespace="/user" extends="struts-default"> 11 12 <!-- 配置默认的行为 ,请求里什么行为都不输入 http://localhost:8080/strutsstu1/user/ --> 13 <default-action-ref name="defaultAction"></default-action-ref> 14 <!-- 全局结果所有的错误都跳转到error.jsp --> 15 <global-results> 16 <result name="myexcept">/error.jsp</result> 17 </global-results> 18 <action name="defaultAction"> 19 <result>/error.jsp</result> 20 </action> 21 <!-- http://localhost:8080/strutsstu1/user/helloStruts?name=admin&age=20 22 package中定义的namespace+action中的name,可以确定一个唯一的访问action的路径 --> 23 <action name="helloStruts" class="cn.test.action.HelloAction"> 24 <result name="success">/success.jsp</result> 25 <result name="error">/error.jsp</result> 26 </action> 27 </package> 28 29 <package name="user" namespace="/user" extends="struts-default"> 30 <action name="login" class="cn.test.action.UserAction" 31 method="login"> 32 <result>/login.jsp</result> 33 <!-- result不写name属性,默认就是success --> 34 </action> 35 <action name="register" class="cn.test.action.UserAction" 36 method="register"> 37 <result>/register.jsp</result> 38 </action> 39 40 <!-- 动态调用 --> 41 <action name="dyTest" class="cn.test.action.User2Action"> 42 <!-- http://localhost:8080/strutsstu1/user/dyTest!login.action --> 43 <result name="gologin">/login.jsp</result> 44 <!--请求地址: http://localhost:8080/strutsstu1/user/dyTest!register.action--> 45 <result name="goregister">/register.jsp</result> 46 </action> 47 48 <!-- 动态调用2 --> 49 <action name="*User" class="cn.test.action.User3Action" 50 method="{1}"> 51 <!--http://localhost:8080/strutsstu1/user/deleteUser.action?value=admin--> 52 <result name="deleteadmin">/{1}admin.jsp</result><!-- deleteadmin --> 53 <result name="deletecommon" type="redirect">/{1}common.jsp</result> <!-- 请求中的*的值组装成deletecommon.jsp--> 54 <!--http://localhost:8080/strutsstu1/user/deleteUser.action?value=adminxxx--> 55 <!-- <result name="deleteerror">/{1}error.jsp</result> --> 56 <!-- --> 57 <result name="deleteerror" type="redirectAction">login</result> <!-- 相当于请求重定向到/user/login --> 58 59 </action> 60 </package> 61 62 63 <package name="service" namespace="/student" extends="struts-default"> 64 <!-- http://localhost:8080/strutsstu1/student/stulogin.action --> 65 <action name="stulogin" class="cn.test.action.StudenAction" 66 method="studentLogin"> 67 <result name="studentLoginSucess">/stuLogin.jsp</result> 68 </action> 69 <action name="sturegister" class="cn.test.action.StudenAction" 70 method="studentRegister"> 71 <result name="studentRegisterSuccess">/stuRegister.jsp</result> 72 </action> 73 74 <action name="stu" class="cn.test.action.StudenAction"> 75 <!--http://localhost:8080/strutsstu1/student/stu!studentLogin.action--> 76 <result name="studentLoginSucess">/stuLogin.jsp</result> 77 <result name="studentRegisterSucess">/stuRegister.jsp</result> 78 </action> 79 </package> 80 81 <package name="ognl" namespace="/ognl" extends="struts-default"> 82 <action name="show" class="cn.test.action.OgnlAction"> 83 <result>/show.jsp</result> 84 </action> 85 <action name="showArray" class="cn.test.action.ArrayAction"> 86 <result name="success">/show.jsp</result> 87 </action> 88 </package> 89 90 <!-- 拦截器 --> 91 <package name="intercepter" namespace="/" extends="struts-default"> 92 <interceptors> 93 <interceptor name="myTime" class="cn.test.interceptor.MyTimeIntercepter"></interceptor> 94 <interceptor-stack name="myTimeStack"> 95 <interceptor-ref name="myTime"></interceptor-ref> <!-- 把自定义的拦截器设置为栈的顶部 --> 96 <interceptor-ref name="defaultStack"></interceptor-ref> 97 </interceptor-stack> 98 </interceptors> 99 <action name="index"> 100 <result>/index.jsp</result> 101 <interceptor-ref name="myTimeStack"></interceptor-ref> <!-- 让myTimeStack这个拦截器栈去进行拦截 --> 102 </action> 103 </package> 104 105 106 </struts>
5.编写物理视图文件success.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib uri="/struts-tags" prefix="s"%> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme() + "://" 6 + request.getServerName() + ":" + request.getServerPort() 7 + path + "/"; 8 %> 9 10 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 11 <html> 12 <head> 13 <base href="<%=basePath%>"> 14 <title>My JSP 'success.jsp' starting page</title> 15 </head> 16 17 <body> 18 This is my success JSP page. 19 <br> 20 <!--<s:property value="name"/> value里面写上Action类中定义的属性,就能在jsp页面显示请求中所带的参数了 --> 21 显示struts action中的属性内容:<br> 22 采用OGNL与Struts2标签输出Action类的属性值(Action类获取到的请求参数值):<br> 23 name=<s:property value="name" /> age=<s:property value="age" /> <br> 24 采用EL表达式 输出Action类的属性值(Action类返回给页面的值):<br> 25 msg=${msg }; 26 </body> 27 </html>
在浏览器中输入 http://localhost:8080/strutsstu1/user/helloStruts?name=admin&age=20 并访问,结果如下,
6.Struts2应用的执行流程简单总结:
(1)浏览器发出user/helloStruts这个请求到服务器(web.xml中配置的url-pattern是/*)
<filter-mapping>
<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
(2)服务器接收后,根据web.xml的配置,将请求分发给指定的Struts2过滤器
(3)过滤器根据Struts.xml的配置内容,将请求发送给cn.test.action.HelloAction类的对象,并调用默认的execute方法、
(4) 根据execute方法的返回结果,在struts.xml文件中匹配 逻辑视图名为success的处理结果(
<result name="success">/success.jsp</result>),当execute方法的返回值为success字符串时,跳转到success.jsp页面
(5)页面根据上下文中的内容,借助Struts2的OGNL表达式和Struts2标签将内容显示在页面上,需要先导入Struts2的标签库:
<%@ taglib uri="/struts-tags" prefix="s"%>
示例代码下载地址 https://github.com/liuch0228/Struts2SSH.git
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步