Struts2的Action访问
● 示例项目结构

● demo1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head> <body>
<h1>Action的访问</h1>
<h3>通过method方法</h3>
<a href="${pageContext.request.contextPath}/userFind.action">查询用户</a>
<a href="${pageContext.request.contextPath}/userUpdate.action">修改用户</a>
<a href="${pageContext.request.contextPath}/userDelete.action">删除用户</a>
<a href="${pageContext.request.contextPath}/userSave.action">保存用户</a><br/> <h3>通过通配符的方法</h3>
<a href="${pageContext.request.contextPath}/product_find.action">查询商品</a>
<a href="${pageContext.request.contextPath}/product_update.action">修改商品</a>
<a href="${pageContext.request.contextPath}/product_delete.action">删除商品</a>
<a href="${pageContext.request.contextPath}/product_sava.action">保存商品</a><br/> <h3>通过动态方法访问</h3>
<a href="${pageContext.request.contextPath}/customer!find.action">查询客户</a>
<a href="${pageContext.request.contextPath}/customer!update.action">修改客户</a>
<a href="${pageContext.request.contextPath}/customer!delete.action">删除客户</a>
<a href="${pageContext.request.contextPath}/customer!sava.action">保存客户</a><br/>
</body>
</html>
dmeo1.jsp
● struts2.xml
<?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>
<include file="com/sve/Struts2/demo2/struts_demo2.xml"/>
</struts>
struts2.xml
● web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Struts2</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> <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>
web.xml
● struts2_demo2.xml
<?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>
<!-- 允许动态方法 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" /> <package name="default" namespace="/" extends="struts-default">
<!-- 通过method方法 -->
<action name="userFind" class="com.sve.Struts2.demo2.UserAction" method="find"></action>
<action name="userUpdate" class="com.sve.Struts2.demo2.UserAction" method="update"></action>
<action name="userDelete" class="com.sve.Struts2.demo2.UserAction" method="delete"></action>
<action name="userSave" class="com.sve.Struts2.demo2.UserAction" method="save"></action> <!-- 通配符的方法 -->
<action name="*_*" class="com.sve.Struts2.demo2.ProductAction" method="{2}"></action> <!-- 动态方法访问 -->
<action name="customer" class="com.sve.Struts2.demo2.CustomerAction"></action>
</package>
</struts>
struts2_demo2.xml
● UserAction.java
package com.sve.Struts2.demo2;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
public String find() {
System.out.println("查询用户。。。");
return NONE;
}
public String update() {
System.out.println("修改用户。。。");
return NONE;
}
public String delete() {
System.out.println("删除用户。。。");
return NONE;
}
public String save() {
System.out.println("保存用户。。。");
return NONE;
}
}
UserAction.java
● CustomerAction.java
package com.sve.Struts2.demo2;
import com.opensymphony.xwork2.ActionSupport;
public class CustomerAction extends ActionSupport {
public String find() {
System.out.println("查询客户。。。");
return NONE;
}
public String update() {
System.out.println("修改客户。。。");
return NONE;
}
public String delete() {
System.out.println("删除客户。。。");
return NONE;
}
public String sava() {
System.out.println("保存客户。。。");
return NONE;
}
}
CUstomerAction.java
● ProductAction.java
package com.sve.Struts2.demo2;
import com.opensymphony.xwork2.ActionSupport;
public class ProductAction extends ActionSupport {
public String find() {
System.out.println("查询商品。。。");
return NONE;
}
public String update() {
System.out.println("修改商品。。。");
return NONE;
}
public String delete() {
System.out.println("删除商品。。。");
return NONE;
}
public String sava() {
System.out.println("保存商品。。。");
return NONE;
}
}
ProductAction.java
1.method方法
<action name="userFind" class="com.sve.Struts2.demo2.UserAction" method="find"></action>
在struts2_demo2.xml中,通过设置action中的method的值,值为class对应的方法名
2.通配符的方法(常用的)
<action name="*_*" class="com.sve.Struts2.demo2.ProductAction" method="{2}"></action>
与method方法略微不同,method的值设置为name值对应星编号,以上也可设置为
<action name="product_*" class="com.sve.Struts2.demo2.ProductAction" method="{1}"></action>
3.动态方法
使用动态方法前必须先设置constant,允许动态方法的使用
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
无须设置method,动态方法是修改访问链接,如示例中的
<action name="customer" class="com.sve.Struts2.demo2.CustomerAction"></action>
<a href="${pageContext.request.contextPath}/customer!find.action">查询客户</a>
customer为action中设置的name值,然后加入“!”,后加上想使用的方法名
Struts2的Action访问的更多相关文章
- struts2的action访问servlet API的三种方法
学IT技术,就是要学习... 今天无聊看看struts2,发现struts2的action访问servlet API的三种方法: 1.Struts2提供的ActionContext类 Object g ...
- struts2学习笔记之六:struts2的Action访问ServletAPI的几种方式
方法一:通过ActionContext访问SerlvetAPI,这种方式没有侵入性 Action类部分代码 import com.opensymphony.xwork2.ActionContext; ...
- Struts2笔记--Action访问Servlet API
Web应用中通常需要访问的Servlet API就是HttpServletRequest.HttpSession和ServletContext,这三个接口分别代表JSP内置对象中的request.se ...
- struts2 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用
Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...
- 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用
Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...
- struts2中的action访问web对象
Struts2的Action就是一个普通的POJO对象,它和Web对象request.response.session和application没有耦合在一起,这样便于单独测试Action,那么我们在A ...
- 4.Struts2中Action的三种访问方式
1.传统的访问方式-很少使用 通过<action>标签中的method属性,访问到action中的具体方法 具体实现: 1.action代码 import com.opensymphony ...
- 访问struts2的action页面出现白板问题
访问struts2的action页面出现白板问题 故需要设置拦截此action的拦截栈, <bean id="authenticationInterceptor" class ...
- struts2的action是多例,servlet是单例
struts2中action是多例的,即一个session产生一个action如果是单例的话,若出现两个用户都修改一个对象的属性值,则会因为用户修改时间不同,两个用户访问得到的 属性不一样,操作得出的 ...
随机推荐
- maven(二),Linux安装maven3.5.3及配置
Linux系统,ubuntu-16.04.4,安装maven3.5.3 一.创建文件夹 注意Linux用户,这个如果不是root用户,命令前面需要加:sudo //创建一个目录 mkdir /usr/ ...
- JavaFx开发桌面软件
JavaFx开发桌面软件 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #839496;} Jav ...
- [fw]IDT表的初始化
IDT表的初始化 linux内核的中断描述符表IDT是一个全局的数据,在i386平台上被定义为: struct desc_struct idt_table[256] __attribute__((_ ...
- Codeforces 492B Name That Tune ( 期望DP )
B. Name That Tune time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- redis出现MISCONF Redis is configured to save RDB snapshots...的错误
今天重启服务器在连接redis数据库时突然报错: MISCONF Redis is configured to save RDB snapshots, but it is currently not ...
- SHOW - 显示运行时参数的数值
SYNOPSIS SHOW name SHOW ALL DESCRIPTION 描述 SHOW 将显示当前运行时参数的数值. 这些变量可以通过 SET 语句来设置,或者通过编辑 postgresql. ...
- TCP/UDP Linux网络编程详解
本文主要记录TCP/UDP网络编程的基础知识,采用TCP/UDP实现宿主机和目标机之间的网络通信. 内容目录 1. 目标2.Linux网络编程基础2.1 嵌套字2.2 端口2.3 网络地址2.3.1 ...
- CentOS7.6系统安装详解(含真机装系统的采坑之旅)!
刚开始学习linux操作系统是总是很茫然,无所适从,以下是自己总结的工作经验,仅供参考! 一.准备资源 安装前需要准备的资源有linux系统centos7.6发行版系统镜像,vmware workst ...
- docker 安装Filebeat
1.查询镜像 docker search filebeat 2.拉取镜像 我此处选择的是prima/filebeat docker pull prima/filebeat 3.创建配置文件 fileb ...
- 链接socket加异常
try { channel = AmqpClient::Channel::Create("10.10.22.105", 5672, "admin", " ...