Struts 2请求映射 [英] Struts 2 request mapping

查看:164
本文介绍了Struts 2请求映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是struts的新手并且正在学习它。我的问题是struts如何使用动作实例变量映射请求参数。例如,我有一个使用用户名和密码的登录表单。现在它向LoginAction发送请求。此操作类如何在用户名实例中获取用户名实例,并在密码变量中获取密码。在servlet中,我们使用 request.getParameter()方法并将值赋给特定变量。

解决方案

我们使用拦截器,在这种情况下struts2将为你设置params param 拦截器,你只需配置你的拦截器堆栈(或者你只需​​使用默认堆栈)。这里是 Interceptor 的链接,如果你在struts2中很新鲜,只需使用默认配置并感受一下。



示例:

 < s:form action =my-action> 
< s:textfield name =myField/>
< s:submit />
< / s:form>



行动类:

  public class MyActionClass扩展ActionSupport {
private String myField; // getter和setter
public String execute()throws Exception {
returnsuccess;
}
}

配置:

 < package name =defaultextends =struts-default> 
< action name =my-actionclass =MyActionClass>
< result> success.jsp< / result>
< / action>
< / package>

在这种情况下 myField 将自动设置拦截器。


I am new in struts and learning it. My question is how struts map request parameter with action instance variable. For Example i have one login form that take username and password. Now it sends request to LoginAction. How this action class will get username in username instance variabel and password in password variable. In servlet we use request.getParameter() method and assign value to particular variable.

解决方案

in struts2 we use Interceptor, in this case struts2 will set the params for you with param interceptor, you just have to config your interceptor stack (or you just use the default-stack). here is the link to Interceptor, if you are very fresh in struts2, just use the default configuration and take a feel.

example:

<s:form action="my-action">
  <s:textfield name="myField"/>
  <s:submit/>
</s:form>

and

action class:

public class MyActionClass extends ActionSupport{
  private String myField; // getter and setter
  public String execute() throws Exception{
    return "success";
  }
}

configuration:

<package name="default" extends="struts-default">
  <action name="my-action" class="MyActionClass">
    <result>success.jsp</result>
  </action>
</package>

in this case myField will be set automatically by interceptor.

这篇关于Struts 2请求映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆