Struts2方法调用的三种方式(默认、多方法、通配符)

 

Struts2方法调用的三种方式(默认、多方法、通配符)


------------------------------------------------------------------------------------------------------------

附加例子:

最新开始学习struts2,在官网上下载的最新的struts2(2.3.15.2), jar包,在使

用动态方法调用的时候老是报错,错误代码如下HTTP Status 404 - There is no 

Action mapped for namespace [/] and action name [book!delBook] associated 

with context path [/Struts2_0900_eg].但是导入struts-2.2.3.1的jar程序就能

正常动态方法调用,最新版的struts2到底怎么实现动态方法调用?代码如下

web.xml配置

XML/HTML code ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<? xml  version = "1.0"  encoding = "UTF-8" ?>
< web-app  version = "2.5" 
     xmlns = "http://java.sun.com/xml/ns/javaee" 
     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
     
     < filter >
         < filter-name >struts2</ filter-name >
         < filter-
 
class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilt
 
er</ filter-class >
     </ filter >
     < filter-mapping >
         < filter-name >struts2</ filter-name >
         < url-pattern >/*</ url-pattern >
     </ filter-mapping >
     
   < display-name ></ display-name >   
   < welcome-file-list >
     < welcome-file >index.jsp</ welcome-file >
   </ welcome-file-list >
</ web-app >


struts.xml配置
XML/HTML code ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<? 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 >
     < package  name = "struts2"  extends = "struts-default" >
         < action  name = "book" 
 
class = "com.softeem.struts.actions.BookAction" >
             < result  type = "redirect" >/index.jsp</ result >
         </ action >
     </ package >
 
</ struts >


jsp页面
  
XML/HTML code ?
1
2
3
4
5
6
< body >
     < a  href = "book!addBook.action" >添加图书信息</ a >< br  />
     < a  href = "book!delBook.action" >删除图书信息</ a >< br  />
     < a  href = "book!updateBook.action" >修改图书信息</ a >< br  />
     < a  href = "book!readBook.action" >查询图书信息</ a >< br  />
   </ body >


Action类
Java code ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package  com.softeem.struts.actions;
 
import  com.opensymphony.xwork2.ActionSupport;
 
public  class  BookAction  extends  ActionSupport {
     public  String addBook() {
 
         System.out.println( "添加图书成功!" );
         return  SUCCESS;
     }
 
     public  String delBook() {
 
         System.out.println( "删除图书成功!" );
         return  SUCCESS;
     }
 
     public  String updateBook() {
 
         System.out.println( "修改图书成功!" );
         return  SUCCESS;
     }
 
     public  String readBook() {
 
         System.out.println( "查询图书成功!" );
         return  SUCCESS;
     }
}
在struts.xml中添加
<constant name="struts.enable.DynamicMethodInvocation" value="true" /> 
打开动态方法调用。
动态方法调用官方推荐的做法是,使用通配符的形式。不要使用actionName!methodName
的方式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值