Struts2一个Action内包含多个请求处理方法的处理

2019-06-17 17:51

Struts2一个Action内包含多个请求处理方法的处理

Struts2一个Action内包含多个请求处理方法的处理(三种方式)

Struts1提供了DispatchAction,从而允许一个Action内包含多个请求处理方法。Struts2也提供了类似的功能。处理方式主要有以下三种方式:

1.1. 动态方法调用:

DMI:Dynamic Method Invocation 动态方法调用。

动态方法调用是指:表单元素的action不直接等于某个Action的名字,而是以如下形式来指定对应的动作名:

则用户的请求将提交到名为”userOpt”的Action实例,Action实例将调用名为”login”方法来处理请求。同时login方法的签名也是跟execute()一样,即为public String login() throws Exception。

注意:要使用动态方法调用,必须设置Struts2允许动态方法调用,通过设置struts.enable.DynamicMethodInvocation常量来完成,该常量属性的默认值是true。

1.1.1. 示例:

修改用户登录验证示例,多增加一个注册用户功能。 1. 修改Action类:

package org.qiujy.web.struts2.action;

import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; /**

*@authorqiujy *@version1.0 */

publicclass LoginAction extends ActionSupport{ private String userName; private String password;

private String msg; //结果信息属性 /**

*@returnthemsg */

public String getMsg() { returnmsg; } /**

*@parammsgthemsgtoset */

publicvoid setMsg(String msg) { this.msg = msg; } /**

*@returntheuserName */

public String getUserName() { returnuserName; } /**

*@paramuserNametheuserNametoset

*/

publicvoid setUserName(String userName) { this.userName = userName; } /**

*@returnthepassword */

public String getPassword() { returnpassword; } /**

*@parampasswordthepasswordtoset */

publicvoid setPassword(String password) { this.password = password; } /**

*处理用户请求的login()方法 *@return结果导航字符串 *@throwsException */

public String login() throws Exception{

if(\\

&&

msg = \登录成功,欢迎\

//获取ActionContext实例,通过它来访问Servlet API ActionContext context = ActionContext.getContext(); //看session中是否已经存放了用户名,如果存放了:说明已经登录了; //否则说明是第一次登录成功

if(null != context.getSession().get(\ msg = this.userName + \:你已经登录过了!!!\ }else{

context.getSession().put(\ }

returnthis.SUCCESS; }else{

msg = \登录失败,用户名或密码错\ returnthis.ERROR; } }

public String regist() throws Exception{ //将用户名,密码添加到数据库中 //...

msg = \注册成功。\ returnthis.SUCCESS; } }

2. struts.xml文件:没有什么变化,跟以前一样配置

\2.0//EN\

\

extends=\

class=\

3. 页面: index.jsp

<%@ page language=\

用户登录页面

name=\


Struts2一个Action内包含多个请求处理方法的处理.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:多体系优化融合 全流程风险防控

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: