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

查看:28
本文介绍了如何在 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 提供了一种简洁的方式来获取你的 action 类中的请求参数,所有你需要遵循这些简单的规则.

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 属性,并且其内置拦截器将在 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天全站免登陆