如何在Action类Struts 2中访问url参数 [英] How to access url parameters in Action classes Struts 2

查看:227
本文介绍了如何在Action类Struts 2中访问url参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java EE和Struts2的新手。我需要知道我是否做错了。

I'm new to Java EE and Struts2. I need to know if I'm doing it wrong or not.

我有这样的链接: http:// localhost:8080 / myProject / deleteUser?idUser = 42

我想要的只是得到idUser值。

All I want is to get the idUser value.

以下是我在动作类中获取参数值的方法:

Here is what I use to get the parameter value in my action class :

HttpServletRequest request = (HttpServletRequest) ActionContext.getContext()
                                  .get(ServletActionContext.HTTP_REQUEST);
System.out.println(request.getParameter("idUser"));


推荐答案

S2提供了一种获取请求参数的简洁方法你需要遵守这些简单规则的动作类。

S2 provides a clean way to fetch the request parameters in you action class all you need to follow these simple rules.


  1. 创建一个与请求参数名称同名的属性。

  2. 为此属性创建getter和setter或将属性设为公共(对于S2.1 +)

S2将检查请求参数并将在您的操作类中查找匹配属性,并将值注入到受尊重的属性中。

S2 will check the request parameter and will look for matching property in your action class and will inject the value in respected property.

在您的情况下所有您需要做的事情

in your case all you need to do

public class MyAction extends ActionSupport{

 private String idUser;
 getter and setters   

}

所以在这种情况下S2会在你的动作类中找到 idUser 属性,它在interceptor中的构建会在 idUser 属性中注入值

So in this case S2 will find the idUser property in your action class and its build in interceptor will inject the value in the idUser property

这篇关于如何在Action类Struts 2中访问url参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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