2009-03-30 17:30:37
一个页面两个按钮,各自作出不同的处理,利用struts2的动态方法调用可以方便地做到这点:
输入页面
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
</head>
<body>
<script language="JavaScript" type="text/javascript">
function regist(){
targetForm = document.forms[0];
//第一种方法,利用Action!methiod
//targetForm.action="Login!regist";
//第二种方法,直接修改action名,修改相应的struts.xml:
//形式1:<action name="Regist" class="com.test.action.PointAction" method="regist"><result...../>...
//形式2:为了减少冗余,采用形式<action name="*Action" class="com.test.action.PointAction" method="{1}">
//但采用形式2,Action类里面不再含有execute()方法,而是分为Login()和Regist()方法
targetForm.action="RegistAction.action";
targetForm.submit();
}
</script>
<h3>
<font color="red">使用逗号将坐标分开</font>
</h3>
<s:form action="LoginAction">
<s:textfield name="point" label="point" />
<s:textfield name="username" label="username" />
<s:textfield name="age" label="age" />
<s:textfield name="date" label="birthday" />
<s:submit label="submit" />
<!-- 第三种方法,直接指定method <s:submit label="注册" method="regist"/> -->
<input type="button" value="注册" onClick="regist();" />
</s:form>
</body>
</html>
struts.xml文件:
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="conveter" extends="struts-default">
<action name="Login" class="com.test.action.PointAction">
<result name="input">/login.jsp</result>
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
<action name="Regist" class="com.test.action.PointAction>
<result name="input">/login.jsp</result>
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
或
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="conveter" extends="struts-default">
<action name="*Action" class="com.test.action.PointAction" method="{1}">
<result name="input">/login.jsp</result>
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
action类:根据方法不同的方法需修改相应代码
package com.test.action;
import java.util.Date;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.test.bean.Point;
public class PointAction extends ActionSupport {
.......
....................
......................
public String Regist() throws Exception {
ActionContext.getContext().getSession().put("user", getUsername());
setTip("恭喜您," + getUsername() + ",您已经注册成功!");
return SUCCESS;
}
@Override
public String execute() throws Exception {
ActionContext ctx = ActionContext.getContext();
Integer counter = (Integer) ctx.getApplication().get("counter");
if (counter == null) {
counter = 1;
} else {
counter += 1;
}
ctx.getApplication().put("counter", counter);
ctx.getSession().put("username", getUsername());
if (getUsername().equals("scott")) {
ctx.put("tip", "登陆成功" + counter + "times");
return SUCCESS;
} else {
ctx.put("tip", "登陆失败" + counter + "times");
return ERROR;
}
}
}
分享到:
相关推荐
Struts2动态方法调用 包括 1.js方法调用 2.普通的表单提交调用 3.通配符
NULL 博文链接:https://takeme.iteye.com/blog/1631525
NULL 博文链接:https://cdxs2.iteye.com/blog/1925232
Struts2_动态方法调用,使用struts2架构的动态方法进行资源调用
主要介绍了详解Struts2动态方法调用,涉及调用方法的代码,具有一定参考价值,需要的朋友可以了解下。
struts 1框架的动态调用也许你会,但是struts2 的动态调用更经典,这个文档让你玩转struts 2 动态调用.......
struts动态访问调用之一,采用method属性,同一个Action内的不同方法来响应用户请求
struts2动态调用之通配符,相当于是改进的method动态调用方法,减少对同一action不同method的多次配置
struts2利用通配符调用同一个Action里面不同的方法,在struts.xml配置文件中将请求方法的不相同部分抽象成“*".
struts2中动态方法的调用,如何实现动态方法调用
本文档主要介绍了struts2中API耦合的三种方法以及动态方法调用(Dynamic Method Invoked)的两种方法,希望对你有所帮助,谢谢!
利用Struts 2框架创建一个web项目chap2_e22,实现用户登录过程。具体要求是在loginAction类中分别用login()和registered()处理用户登录和注册的过程,分别创建login.jsp和register.jsp两个页面实现登录和注册的...
带有分页,分页跳转,部门员工表的CRUD,hibernate注解方式操作实体,struts2动态方法调用
Struts2中直接和间接访问ServletAPI以及动态方法调用之感叹号
Struts2调用EJB异常项目包,出现的问题以及解决方案参见EJBStruts.zip中的说明.txt 以及博客 http://blog.csdn.net/icecream0/article/details/7167853
struts2实验一:动态方法调用+请求参数的获取+简单的表单数据验证
主要介绍了struts中动态方法调用使用通配符的相关资料,非常不错,具有参考借鉴价值,感兴趣的朋友一起看看吧
参考博文:http://blog.csdn.net/u011638419/article/details/41773411
Webservice+spring+struts小例子,简单方法调用
如题,Struts2登陆+输入验证+动态调用实例,使用MyEclipse导入直接就可以运行