● 示例项目结构

●  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访问的更多相关文章

  1. struts2的action访问servlet API的三种方法

    学IT技术,就是要学习... 今天无聊看看struts2,发现struts2的action访问servlet API的三种方法: 1.Struts2提供的ActionContext类 Object g ...

  2. struts2学习笔记之六:struts2的Action访问ServletAPI的几种方式

    方法一:通过ActionContext访问SerlvetAPI,这种方式没有侵入性 Action类部分代码 import com.opensymphony.xwork2.ActionContext; ...

  3. Struts2笔记--Action访问Servlet API

    Web应用中通常需要访问的Servlet API就是HttpServletRequest.HttpSession和ServletContext,这三个接口分别代表JSP内置对象中的request.se ...

  4. struts2 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用

    Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...

  5. 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用

    Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...

  6. struts2中的action访问web对象

    Struts2的Action就是一个普通的POJO对象,它和Web对象request.response.session和application没有耦合在一起,这样便于单独测试Action,那么我们在A ...

  7. 4.Struts2中Action的三种访问方式

    1.传统的访问方式-很少使用 通过<action>标签中的method属性,访问到action中的具体方法 具体实现: 1.action代码 import com.opensymphony ...

  8. 访问struts2的action页面出现白板问题

    访问struts2的action页面出现白板问题 故需要设置拦截此action的拦截栈, <bean id="authenticationInterceptor" class ...

  9. struts2的action是多例,servlet是单例

    struts2中action是多例的,即一个session产生一个action如果是单例的话,若出现两个用户都修改一个对象的属性值,则会因为用户修改时间不同,两个用户访问得到的 属性不一样,操作得出的 ...

随机推荐

  1. shell编程:sed的选项

    sed [参数] [partern/commond] file 标准输出 | sed sed [参数] [partern/commond] -n :使用安静(silent)模式.在一般 sed 的用法 ...

  2. Autofac基本使用

    原文:Autofac基本使用 AutoFac是.net平台下的IOC容器产品,它可以管理类之间的复杂的依赖关系.在使用方面主要是register和resolve两类操作. 这篇文章用单元测试的形式列举 ...

  3. docker 部署vsftpd服务、验证及java ftp操作工具类

    docker部署vsftpd服务 新建ftp文件存储目录/home/ftp cd /home mkdir ftp 创建一个组,用于存放ftp用户 groupadd ftpgroups 创建ftp用户, ...

  4. vue 改变某个页面的背景色

    beforeCreate(){ // 添加背景色 document.querySelector('body').setAttribute('style', 'background-color:#fff ...

  5. Sass--伪类嵌套

    其实伪类嵌套和属性嵌套非常类似,只不过他需要借助`&`符号一起配合使用. a { &:link, &:visited { color: blue; } &:hover ...

  6. JavaScript仿淘宝实现放大镜效果的实例

    我们都知道放大镜效果一般都是用于一些商城中的,列如每当我们打开淘宝,天猫等pc端时,看到心仪的物品时,点击图片时,便呈现出放大镜的效果.在没有去理解分析它的原理时,感觉非常的神奇,当真正地去接触,也是 ...

  7. ubuntu openssl 生成密钥对

    一般情况下ubuntu和mac系统都会自带openssl,安装之前先测试一下,打开终端,输入openssl,如果出现以下画面,即已安装. root@jiang:/home/kevin/work/ope ...

  8. Future模式的简单实现

    /** * 数据接口 */ public interface Data { public String getResult(); } /** * 最终需要使用的数据模型 */ public class ...

  9. UVa 11806 Cheerleaders (容斥原理+二进制表示状态)

    In most professional sporting events, cheerleaders play a major role in entertaining the spectators. ...

  10. 【软工项目Beta阶段】博客目录

    绝不划水队Beta冲刺阶段博客目录 一.Scrum Meeting 第十周会议记录 第十一周会议记录 二.测试报告 Beta阶段测试报告 三.习得的软工原理/方法/技能? (1)在进行OUC-Mark ...