struts的动态方法调用

1.首先在struts.xml文件中设置支持动态方法的调用

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!--动态方法调用设置为true支持-->
<span style="white-space:pre">	</span><constant name="struts.enable.DynamicMethodInvocation" value="true">
<span style="white-space:pre">	</span><package name="my" extends="struts-default" namespace="/">
<span style="white-space:pre">		</span><action name="test" class="com.itany.action.UserAction">
<span style="white-space:pre">			</span><result>/success.jsp</result>
<span style="white-space:pre">			</span><result name="error">/error.jsp</result>
<span style="white-space:pre">		</span></action>
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span><action name="reg" class="com.itany.action.UserAction">
<span style="white-space:pre">			</span><result>/registesuccess.jsp</result>
<span style="white-space:pre">		</span></action>
<span style="white-space:pre">	</span></package>
<pre name="code" class="html"></struts>

 

前台页面jsp代码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script type="text/javascript">
		function changeUrl()
		{
			tagform=document.forms[0];   //获取第一个form保单
			tagform.action="reg!registe.action?msg=qwe";
			tagform.submit();
		}
	</script>
  </head>
  
  <body>
  
  <!-- 感叹号说明调用的是哪个方法,格式是actionName!methodName.action  -->
  	<form action="test!login.action" method="post">
  		username:<input type="text" name="username" /><br/>
  		password:<input type="password" name="password"/><br/>
  		<input type="submit" value="submit"/> 
  		<input type="button" value="registe" οnclick="changeUrl()"/>
  	</form>
  </body>
</html>


UserAction代码:

package com.itany.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport 
{
	private String msg;
	
	public String getMsg() {
		return msg;
	}


	public void setMsg(String msg) {
		this.msg = msg;
	}


	/**
	 * 动态方法调用
	 * <一句话功能简述>
	 * <功能详细描述>
	 * @return
	 * @throws Exception
	 * @see [类、类#方法、类#成员]
	 */
	public String login() throws Exception
	{
		System.out.println("动态方法调用");
		HttpServletRequest request=ServletActionContext.getRequest();
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		if("qwe".equals(username)&&"qwe".equals(password))
		{
			msg="登录成功,欢迎"+username;
			return SUCCESS;
		}
		else
		{
			msg="没有"+username+"用户名信息!";
			return ERROR;
		}
		
	}
	
	public String registe() throws Exception
	{
		System.out.println("注册方法调用。。。。。。");
		HttpServletRequest request=ServletActionContext.getRequest();
		msg+="注册成功!";
		return SUCCESS;
	}
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值