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>
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;
}
}