动态访问某个方法
来源:互联网 发布:搭建阿里云服务器 编辑:程序博客网 时间:2024/06/09 16:36
如果我们想要访问某个action中的某个方法,而不是execute可以在xml加上method的设置,但是我想要动态的访问,就是自己想去哪个方法就去那个方法可怎么设置呢。
首先在struts.xml中增加一行
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
如果已经有了这一行就把他设置成true,意思是是否支持动态方法(这样做存在一点点的安全隐患)
首先我们创建一个action的类,里面有method1和method2方法。内容如下
package action;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport {@Overridepublic String execute() throws Exception {// TODO Auto-generated method stubSystem.out.println("This is execute");return super.execute();}public String method1() throws Exception {System.out.println("This is one");return SUCCESS;}public String method2() throws Exception {System.out.println("This is two");return SUCCESS;}}
之后在jsp页面中这样写,我这里举了三个简单的例子,一个是直接通过不同的action来访问到不同的method,还有就是一个列表来选择需要访问的method,最后一个是使用通配符来访问method
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html><html><head><meta charset="utf-8"><title></title></head><body><!-- 第一个表单访问的是login所对应的class中的method1方法,第二个是method2方法--><s:form action="login!method1"><s:submit value="method1"/></s:form><s:form action="login!method2"><s:submit value="method2"/></s:form><hr><!-- 使用select实时的去选择要访问的方法 --><s:form id="myForm"><s:select list="#{'method1':'method1', 'method2':'method2'}" id="mySelect"/><s:submit onclick="select()"/></s:form><hr><!-- 使用通配符(*)来访问某个方法 ,与之类似的,类也可以用这样访问--><s:form action="method1login"><s:submit value="one"/></s:form><s:form action="method2login"><s:submit value="two"/></s:form></body><script type="text/javascript">function select() {//把myForm的action修改为需要的actiondocument.getElementById("myForm").action = "Login!"+document.getElementById("mySelect").value;document.getElementById("myForm").submit();}</script></html>
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" /> <constant name="struts.devMode" value="true" /><constant name="struts.custom.i18n.resources" value="test"/> <package name="default" namespace="/" extends="struts-default"> <action name="Login" class="action.LoginAction"> <result>/content/success.jsp</result> </action> <!-- 此处的意思是只要结尾是login的表单的action都能够符合这个action,都会访问到LoginAction这个类 举例来说,如果是一个action为testlogin的表单,那么这里的{1}就是test,也就会访问到 LoginAction中的test() 如果需要通配不同类的不同方法可以用多个*号,比如说这样 <action name="*Action*" class="action.{1}Action" method={2}> 这样当我用LoginActioncheck去访问的时候就可以通过这个action的通配,就可以访问到 action.LoginAction这个类的check(),更多*号的与之类似 --> <action name="*login" class="action.LoginAction" method="{1}"> <result>/content/success.jsp</result> </action> </package></struts>
0 0
- 动态访问某个方法
- 动态访问方法
- 动态方法访问
- ajax能访问struts action中特定的某个方法
- nginx禁止某个IP访问站点的设置方法
- Extjs 动态隐藏某个textfield以及fieldLabel组件的方法
- jdk动态代理,统计某个方法的执行时间
- JAVA访问C++动态库的方法
- JAVA动态访问类方法和属性
- struts2 动态访问Action方法NoSuchMethodException问题
- mysql 访问某个分区
- 09-防止用户未登录非法访问某个页面的方法
- Windows Server 2008 R2通过IP安全策略阻止某个IP访问的设置方法
- SpringMVC 限定某个业务控制方法,只允许GET或POST请求方式访问
- 使用Semaphore控制某个方法允许并发访问的线程的个数
- 如何一启动web程序,直接访问某个controller里的方法进而跳转页面
- 【SpringMVC】限定某个业务控制方法,只允许GET或POST请求方式访问(十二)
- 屏蔽某个文件被访问!!
- Android ListView的addHeaderView与addFooterView的详解
- 利用ViewPager、PagerTabStrip实现滚动导航
- HDOJ1010.Tempter of the Bone
- PHP strtr与str_replace函数性能简单测试
- App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Te
- 动态访问某个方法
- android 开发 小技巧
- 利用border-radius属性写个简单的左右内弧,上下外弧的框
- 【必看】用户画像数据建模方法
- 小马哥---高仿红米note 刷机 主板型号T7203主板图与开机识别图 6572芯片
- 深入hibernate的三种状态
- Cocos2d-swift V3.x 中的update方法
- ubuntu14.04下载android source code
- app 提交 app store 遇到的问题