01:web.xml中配置,启动struts2

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><!-- 01:启动struts2框架 --><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><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>
</web-app>

02:编写action类

package com.self.action;
/*** 02:写相应的处理方法*/
public class HelloWorldAction {private String message;public String helloworld_1(){this.message="helloworld_1";return "helloworld_1";}public String helloworld_2(){this.message="helloworld_2";return "helloworld_2";}//为属性注入值,需要提供set方法public void setMessage(String message) {this.message = message;}//在页面显示值,需要get方法public String getMessage() {return message;}}

03:配置struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts><!-- 01:将.action访问,改为.do和.action --><constant name="struts.action.extension" value="do,action" /><!-- 02:指定默认编码,相当于HttpServletRequest的setCharacterEncoding方法,也作用于freemarker、velocity的输出 --><constant name="struts.i18n.encoding" value="UTF-8" /><include file="department.xml"/>
</struts>

  

04:编写引入的department.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts><package name="dep" namespace="/department" extends="struts-default"><!-- 用通配符*来指代方法名,{1}代表第一个通配符所代表的字段:这里代表方法 --><action name="helloworld_*" class="com.self.action.HelloWorldAction" method="{1}" ><result name="helloworld_1">/helloworld_1.jsp</result><result name="helloworld_2">/helloworld_2.jsp</result></action></package>
</struts>

  

05:编写界面1

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head><title>显示</title>
</head><!-- 第4步:显示 -->
<body><BR><BR><center>将页面放在WEB-INF下面,这样用户直接访问不到<BR> <BR> 为action的属性注入值: ${message}<BR></center>
</body>
</html>

  

06:编写界面2

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head><title>显示</title>
</head><!-- 第4步:显示 -->
<body><BR><BR><center>将页面放在WEB-INF下面,这样用户直接访问不到<BR> <BR> 为action的属性注入值: ${message}<BR></center>
</body>
</html>

  

07:访问路径1、2

http://localhost:8080/Struts2_01/department/helloworld_helloworld_1.action

http://localhost:8080/Struts2_01/department/helloworld_helloworld_2.action

转载于:https://www.cnblogs.com/zjsy/p/4205845.html

struts配置通配符*来匹配方法,实现动态调用相关推荐

  1. java 动态调用方法_Java动态调用方法

    public void function(String str) { //根据 str的值 调用相应的方法 } public void test() { //code } 如str的值为test,就调 ...

  2. 动态调用对象的属性和方法——性能和灵活性兼备的方法

    在动态编程时,我们常常需要运行时确定调用对象的哪个属性或哪个方法.这个任务通常可以用反射来解决.但众所周知,反射的性能要比静态指定的方式低很多,因为反射要通过运行时复杂的机制完成.能否获得性能和灵活性 ...

  3. js 动态调用 applet 内的方法

    js 动态调用 applet 内的方法 js 动态调用 applet 内的方法示例程序下载链接         js.applet(java) 都是客户端脚本语言,在客户端与用户进行着一些信息交互.然 ...

  4. struts 2 配置通配符

    2019独角兽企业重金招聘Python工程师标准>>> 随着Web应用程序的增加,所需的Action也会更多,从而导致大量的action映射,使用通配符可以减少action配置的数量 ...

  5. EXCEL表格-VLOOKUP多对一结果匹配方法(通配符)

    ❤关注我,不迷路❤ 点击进入EXCEL综合应用场景专栏 在实际使用场景中,通过一个值去匹配另一个值的案例很常见,比如一份学校的信息表,通过姓名查找班级.家长姓名等,均用VLOOKUP函数可以实现,正向 ...

  6. struts配置详解

    0.struts 配置加载顺序问题: default.properties  ->  struts-default.xml  -> struts-plugin.xml  -> str ...

  7. Spring AOP根据JdbcTemplate方法名动态设置数据源

    2019独角兽企业重金招聘Python工程师标准>>> 说明:现在的场景是,采用数据库(Mysql)复制(binlog)的方式在两台不同服务器部署并配置主从(Master-Slave ...

  8. vue路由添加.html,怎么使用VueRouter的addRoutes方法实现动态添加用户的权限路由

    怎么使用VueRouter的addRoutes方法实现动态添加用户的权限路由 发布时间:2021-04-26 13:34:44 来源:亿速云 阅读:76 作者:小新 小编给大家分享一下怎么使用VueR ...

  9. Struts2中Action的动态调用方法

    在Struts2中,Action执行的时候并不一定要执行execute,我们可以指定Action执行哪个方法,下面分别介绍三种方法来指定Action执行哪个方法: 1.第一种方法,通过Action里的 ...

最新文章

  1. 宏基因组合种树,2-4天领证,1/2号车满员,3号车成立,机会来了
  2. 维基链Defi时代,生态完善,持续输出
  3. Web应用性能分析工具—HAR文件
  4. python的魔法_python魔法方法大全
  5. 作业帮电脑版在线使用_一起作业学生app 手机版免费在线下载
  6. js基础知识温习:构造函数与原型
  7. 绝杀《绝地求生》外挂!
  8. 查询数据表中重复记录
  9. 多参selector
  10. Security+ 学习笔记46 网络工具
  11. 重标极差(R/S)分析法计算Hurst指数(Python)
  12. 计算机考研对英语四六级有要求吗,2022考研指南|考研对英语四六级有要求吗?...
  13. nginx -rtmp多码率,动态码率二级m3u8适应
  14. 有多久没有这么疯狂了?
  15. 苹果7手机通讯录删除了还能够找回来么
  16. Unity3D实现谷歌数字地球
  17. A卡 HD5770(HD6770) Readon HD 5xxx 6xxx系列显卡黑苹果MOJAVE CATALINA QE/CI驱动教程
  18. 一周新闻纵览:工信部组织召开综合整治骚扰电话专项行动;智能锁百万指纹泄密;4G不会降速5G网速会更快
  19. 软编码Flv 到Mp4 容器(十三) fmp4 生成ftyp和moov所必要的 flv数据
  20. Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null objec

热门文章

  1. “弱肉强食,月半者为王” | OpenDILab推出多智能体博弈环境Go-Bigger
  2. 四川长虹招聘机器视觉、图像识别工程师
  3. ​使用高斯过程回归指导网络轻量化
  4. ICCV 2019 | 华科提出对称性约束的校正网络ScRN,显著改进场景文本识别
  5. 历史精华文章分类汇总,祝大家中秋快乐!
  6. 快速排序c语言单链表代码,快速排序算法及源代码(C语言)
  7. 想轻松入门Python编程,必须看这10个经典案例,学完就能找到工作
  8. arXiv与文献调研神器Connected Papers强强联合
  9. 蒙特利尔大学助理教授唐建《图表示学习:算法与应用》研究进展
  10. GoogLeNet的心路历程(三)