struts2通配符的使用

                                       Struts2之通配符

前言:

众所周知struts.xml文件的配置是struts2框架搭建必不可少的一环,所以配置struts.xml文件就是搭建struts2框架的日常,期间的各种报错也是在所难免的,当碰到:

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">

前面出现黄色感叹号的时候大部分是dtd文件引入失败。

出现这种情况的原因有两种:

 第一种 你写错了。第二种 没有网络。

 

下面讲一下struts2通配符的使用

Jsp页面代码:

 

<a href="add_UserAction.action">add</a>

<a href="del_UserAction.action">del</a>

<a href="cc_UserAction.action">cc</a>

Action类的代码:

package xyz.askway.action;

 

public class UserAction {

   

 

    public String cc() {

       System.out.println("ccc");

       return "return";

    }

    public String add() {

       System.out.println("add");

       return "return";

    }

    public String del() {

       System.out.println("del");

       return "return";

    }

 

}

struts.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" />

 

   <!-- 所有匹配*.action的请求都由struts2处理 -->

  <constant value="action" name="struts.action.extension"/>

 

 

    <!-- 开发模式下使用,这样可以打印出更加详细的错误信息 -->

    <constant name="struts.devMode" value="true"></constant>

    <!-- 默认视图主题 -->

    <constant name="struts.ui.theme" value="simple"></constant>

    <!-- name属性:包名,用于被别的包调用或继承

        extends: 继承哪个包,会继承该包下配置信息和拦截器等等

        namespace:选填,url连接必须加入/new/action.xxx

     -->

  <package name="default" extends="struts-default">

 

<!--‘*’代表通配符,{1}里面的数字代表取第几个*号的值 。架设选择用户点击的是add的a标签,那么{1}取到的值就是add  {2}取到的值就是UserAction -->

 

      <action name="*_*" class="xyz.askway.action.{2}" method="{1}">

         <result name="return">index.jsp</result>

<!--<allowed-methods>这里写你的方法名不同的方法用豆号间隔</allowed-methods> 注意:这一段一定不能少-->

        <allowed-methods>add,del,cc</allowed-methods>

      </action>

     

   </package>

 

 

</struts>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值