Struts2入门——4,访问Action
来源:互联网 发布:nginx grpc 编辑:程序博客网 时间:2024/05/02 02:26
HTTP请求提交struts2 StrutsPrepareAndExecuteFilter 核心控制器 ——-请求分发给不同的Action
1.让请求能够访问Action ——-Action书写方式 三种
1)Action是POJO(简单的java对象)类—-不需要继承任何父类,不需要实现任何接口.
例子:
在项目中新建request.jsp、RequestAction.java。在struts.xml配置<action>
。
request.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!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=utf-8"><title>Insert title here</title></head><body><h1>访问请求页面</h1><a href="${pageContext.request.contextPath}/request1.action">请求一</a></body></html>
RequestAction.java
package com.cn.struts2.action;public class RequestAction { public String execute(){ return null; }}
struts.xml添加的<action>
配置
<action name="request1" class="com.cn.struts2.action.RequestAction"> </action>
然后启动服务器,访问地址http://localhost:8080/web/jsp/request.jsp 然后点击带有超链接struts2会跳转到一个空白页面,则运行成功。
2).编写Action实现action接口
request.jsp添加
<a href="${pageContext.request.contextPath}/request2.action">请求二</a>
新建RequestAction2.java
package com.cn.struts2.action;import com.opensymphony.xwork2.Action;//继承Action接口,引入的包为xwork2的Action包public class RequestAction2 implements Action{ @Override public String execute() throws Exception { System.out.println("request2"); return null; } }
struts.xml添加
<action name="request2" class="com.cn.struts2.action.RequestAction2"></action>
访问地址http://localhost:8080/web/jsp/request.jsp 然后点击带有超链接“请求二”会跳转到一个空白页面,则运行成功。
PS:Action接口中定义了5中逻辑视图名称
public static final String SUCCESS = "success"; //处理数据成功(成功页面)public static final String NONE = "none";//页面不跳转和return null;效果一样public static final String ERROR = "error";//数据处理错误(错误页面)public static final String INPUT = "input"; //用户输入数据有误,通常用于表单数据校验(输入页面)public static final String LOGIN = "login"; //主要权限认证(登录页面)
3).编写Action继承ActionSupport
在request.jsp中添加
<a href="${pageContext.request.contextPath}/request3.action">请求三</a>
新建success.jap页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!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=utf-8"><title>Insert title here</title></head><body> <h1>success</h1></body></html>
新建RequestAction3.java
package com.cn.struts2.action;import com.opensymphony.xwork2.ActionSupport;public class RequestAction3 extends ActionSupport{ @Override public String execute() throws Exception { return super.execute(); }}
在struts.xml中添加
<action name="request3" class="com.cn.struts2.action.RequestAction3"> <result name="success">/jsp/success.jsp</result> </action>
访问地址http://localhost:8080/web/jsp/request.jsp 然后点击带有超链接“请求三”会跳转到一个有Success的页面,则运行成功。
继承ActionSupport类在Action中使用表单验证、错误信息设置、读取国际化信息三个功能。
2.Action的方法调用
1).在配置<action>
元素时,没有指定method属性,默认执行Action类中的execute()方法
2).在配置<action>
元素内部添加method属性,指定执行Action中哪个方法,则执行时便执行指定的方法
如:<action name="regist" class="cn.itcast.struts2.demo4.RegistAction" method="regist"/>
执行时便执行指定的regist()方法。这样我们便可以将多个请求业务方法写到一个Action类中。
我们来写一个例子看一下:
新建BookAction.java
package com.cn.struts2.action;import com.opensymphony.xwork2.ActionSupport;public class BookAction extends ActionSupport{ public String addBook(){ return NONE;//NONE跳转到空白页面 } public String delBook(){ return NONE;//NONE跳转到空白页面 }}
新建book.jsp文件
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!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=utf-8"><title>Insert title here</title></head><body><h1>Book</h1><a href="${pageContext.request.contextPath}/addbook.action">添加图书</a><a href="${pageContext.request.contextPath}/delbook.action">删除图书</a></body></html>
在struts.xml中添加
<action name="addbook" class="com.cn.struts2.action.BookAction" method="addBook"></action> <action name="delbook" class="com.cn.struts2.action.BookAction" method="delBook"></action>
PS:注意<action>
中的method属性值与BookAction中的方法名相同。
访问:http://localhost:8080/web/book.jsp 访问页面,然后点击添加图书,会跳出空白页面;返回点击删除图书,会跳转到空白页面。即表示成功了。
3).使用通配符”*”来简化struts.xml配置
通过一个例子来看一下:
新建CustomerAction.java
package com.cn.struts2.action;import com.opensymphony.xwork2.ActionSupport;public class CustomerAction extends ActionSupport{ public String add(){ return NONE; } public String list(){ return NONE; } public String update(){ return NONE; } public String delete(){ return NONE; }}
新建customer.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!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=utf-8"><title>Insert title here</title></head><body><h1>Customer</h1><a href="${pageContext.request.contextPath}/customer_add.action">添加客户</a><a href="${pageContext.request.contextPath}/customer_list.action">客户列表</a><a href="${pageContext.request.contextPath}/customer_update.action">修改客户</a><a href="${pageContext.request.contextPath}/customer_delete.action">删除客户</a></body></html>
然后在struts.xml中添加
<action name="customer_*" class="com.cn.struts2.action.CustomerAction" method="{1}"></action><!-- 1对应第一个*号 -->
然后访问http://localhost:8080/web/jsp/customer.jsp 然后点击添加客户、客户列表、修改客户、删除客户,都会跳转到空白页面,不报错则表示成功了。
4).动态方法调用
访问Action中指定方法,不进行配置
a.在工程中使用,动态调用,首先更改struts.xml中的<constant name="struts.enable.DynamicMethodInvocation" value="true" />
保证value为true。
b.在action的访问路径 中 使用 “!方法名” 。新建product.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!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=utf-8"><title>Insert title here</title></head><body><h1>Product</h1><a href="${pageContext.request.contextPath}/product!add.action">添加客户</a><a href="${pageContext.request.contextPath}/product!del.action">删除客户</a></body></html>
c.新建ProductAction.java
package com.cn.struts2.action;import com.opensymphony.xwork2.ActionSupport;public class ProductAction extends ActionSupport{ public String add(){ return NONE; } public String del(){ return NONE; }}
d.在struts.xml中配置action
<action name="product" class="com.cn.struts2.action.ProductAction"></action>
- Struts2入门——4,访问Action
- Struts2—Action方法的访问
- struts2学习(11)——action访问ServletAPI
- Struts2学习笔记(六)——Action访问Servlet API
- Struts2(4):Action访问Servlet API---ActionContext
- struts2——action!!!
- Struts2—Action
- Struts2入门(4): Action 动作
- 2-struts2入门-action
- Struts2 Action的访问路径
- Struts2中Action访问ServletAPI
- Struts2 Action的访问路径
- struts2 Action访问Servlet Api
- Struts2--------Action访问Servlet API
- Struts2 Action访问Servlet API
- 【3】Struts2的Action访问
- Struts2-Action访问Servlet API
- Struts2中Action访问ServletAPI
- iOS常用加密方法
- 十五.Android网络通信与Volley简单介绍
- bzoj 2927 博弈论
- greendao 数据库工程里使用的问题 database from version 3 to 1
- 通过IIS配置网站和相关错误处理
- Struts2入门——4,访问Action
- 找出出现次数大于等于总数一半的数 (O(1)空间 O(n)时间)
- [LeetCode] 152. Maximum Product Subarray
- 声明
- Make编译学习笔记(三)Makefile基本规则
- Perl语言入门(09 perl中自带的函数)
- 结构体指针分析
- 最长严格上升子序列
- HTML5所有标签汇总