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

struts配置通配符*来匹配方法,实现动态调用的更多相关文章

  1. Struts 之 通配符 路径匹配 常量用法 配置默认值

    Struts 框架学习 Action的开发的几种方式 方式1 : 继承ActionSupport     如果使用Struts校验功能,必须继承此类 方式2 : 实现Action接口 方式3 :不继承 ...

  2. new关键字在虚方法的动态调用中的阻断作用

    关于new关键字在虚方法动态调用中的阻断作用,也有了更明确的理论基础.在子类方法中,如果标记 new 关键字,则意味着隐藏基类实现,其实就是创建了与父类同名的另一个方法,在编译中这两个方法处于动态方法 ...

  3. struts2学习笔记(2)action多个方法的动态调用

    ①在struts.xml中的action添加method <action name="addhelloworld" method="add" class= ...

  4. struts2学习笔记之八:Action中方法的动态调用

    方法一:action名称+!+方法名称+后缀 Action类中增加addUser()和delUser()方法, package com.djoker.struts2; import org.apach ...

  5. struts2_7_Action类中方法的动态调用

    (一)直接调用方法(不推荐使用) 1)Action类: private String savePath; public String getSavePath() { return savePath; ...

  6. java根据方法名动态调用invoke方法!

    public class Activity { public void deal(String name, long id) { System.out.println(name + id + &quo ...

  7. Struts.xml中Action的method与路径的三种匹配方法

    原文  http://blog.csdn.net/woshixuye/article/details/7734482 首先我们有一个Action——UserAction public class Us ...

  8. java:struts框架2(方法的动态和静态调用,获取Servlet API三种方式(推荐IOC(控制反转)),拦截器,静态代理和动态代理(Spring AOP))

    1.方法的静态和动态调用: struts.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCT ...

  9. 10、一个action中处理多个方法的调用第一种方法动态调用

    我们新建一个用户的action package com.weiyuan.test; import com.opensymphony.xwork2.ActionSupport; /** * * 这里不用 ...

随机推荐

  1. random and password 在Linux下生成crypt加密密码的方法,shell 生成指定范围随机数与随机字符串

    openssl rand -hex n (n is number of characters) LANG=c < /dev/urandom tr -dc _A-Z-a-z-0-9 | head ...

  2. SQLite.net发布后找不到"SQLite.Interop.dll"的问题

    http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki sqlite-netFx40-static-binary-b ...

  3. java对象中继承和变量初始化顺序浅析

    先上例子代码 public class F { int age = 5; public F() { print(); } public void print() { System.out.printl ...

  4. [LeetCode]题解(python):051-N-Queens

    题目来源 https://leetcode.com/problems/n-queens/ The n-queens puzzle is the problem of placing n queens ...

  5. 多大开始玩EV3

    机器人EV3还是初中生才能玩的溜 耐撕爸爸推荐年龄:初中及以上 推荐理由:无需使用计算机就可进行编程,培养孩子的编程.机械.电子.物理等综合能力. 在欧美等国家,乐高机器人融入教学已成为一种普遍现象, ...

  6. django中嵌入百度editor插件

    一.安装和配置步骤: 1.先下载百度ueditor插件,并安装pip install DjangoUeditor 2.把下载好的ueditor插件放到自己的项目中 3.配置setting INSTAL ...

  7. 8款实用Sublime text 3插件推荐

    Sublime Text作为一个尽为人知的代码编辑器,其优点不用赘述.界面整洁美观.文本功能强大,且运行速度极快,非常适合编写代码,写文章做笔记.Sublime Text还支持Mac.Windows和 ...

  8. iOS多线程知识总结--GCD

    iOS多线程知识总结--GCD 1. iOS中苹果提供4钟方案来帮助我们实现多线程: (1) 纯C语言的pthread,偏底层,需要程序员手动管理线程的生命周期,基本不用. (2) OC语言的NSTr ...

  9. Silverlight打印注意事项

    1.Silverlight的打印功能从版本5开始才支持矢量打印,这不但要求打印机支持矢量打印,而且还要安装相应的打印驱动程序. 测试你的打印机是否支持矢量打印,可以参考:如何用C#代码检测打印机和驱动 ...

  10. MySql避免重复插入记录方法(ignore,Replace,ON DUPLICATE KEY UPDATE)

    ON DUPLICATE KEY UPDATE  博客 http://blog.csdn.net/jbboy/article/details/46828917