Struts 2请求参数验证(整数和字符串) [英] Struts 2 Request Parameter Validations (Int and Strings)

查看:72
本文介绍了Struts 2请求参数验证(整数和字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说我在这里有这个网址

Let's say I have this URL here

<s:url action ="profile" var ="profile_url">
  <s:param name = "id">${user.userId}</s:param>
</s:url>
<s:a href = "%{profile_url}">My Profile</s:a>

其中参数id仅具有int值.所以在我的Action类中.

Where the parameter id will only have an int value. So inside my Action class.

public class ViewProfileAction extends ActionSupport{

    public String execute(){
       //someServiceLayer.getUser(id);
       return "success";
    }

    public int getId() {
       return id;
    }

    public void setId(int id) {
       this.id = id;
    }

    private int id;
}

只要用户单击链接,一切似乎都会顺利进行,因此,如果用户单击链接,则网址将是这样

Everything seems to go well as long as the user will click the link, so if the user click the link the url will be something like this

localhost:8090/HelloStruts2/profile?id=1

但是,如果用户直接操纵URL,该怎么办?他是在浏览器中手动输入字母还是字符?像这样

but what if the user manipulated the URL directly? he manually typed in his browser a letter or a character? like this

localhost:8090/HelloStruts2/profile?id=b

如果用户确定我肯定会出现异常或发生错误.

if the user did that I am sure there will be an exception or an error will occur.

我的问题然后将如何验证URL参数?或者,如果用户执行了此操作(在参数id中输入字母或负数),我会将其重定向到另一页.

My question how would I then validate the URL parameter? or if the user did such thing (entered a letter or a negative number in the parameter id) I would redirect him to another page.

推荐答案

您在URL中将参数字段设置为int,但已将其设置为String.要消除此类错误,您需要在params拦截器之前(或之后)放置一些拦截器以检查该值.例如,您放置了validation拦截器.

You have a parameter field as int but you have set it as String in the URL. To eliminate the errors of that kind you need to put some interceptor before (or after it doesn't matter) the params interceptor to check that value. For example you put the validation interceptor.

@Action(value = "youraction", results = {
  @Result(location = "/path/to/page.jsp"),
}, interceptorRefs = {@InterceptorRef("validation"), @InterceptorRef("basicStack")})

然后您需要在操作中实现validate()方法

then you need implement validate() method in your action

public void validate() {}

最后添加将处理错误的方法,并且效果良好,因此OGNL不会抱怨

and finally add the method that will handle wrong and also good results, so OGNL will not complain

public void setId(String id) {
    try {
      this.id = Integer.parseInt(id);
    } catch (NumberFormatException nfe){}
} 

仅此而已,您也不再例外,并且在设置器中检查了类型为String的参数.

that's all, you have no exception anymore and parameter of type String is checked in the setter.

问题:如果未正确解析参数,则execute()方法中的值将为0.因此,您决定要返回什么结果.如果放置在拦截器之后的validation拦截器进行解析或在setFieldError()之后的GenericValidator.isInt进行解析并在JSP中显示它或在验证后立即重定向,您还可以在validate()方法中检查该值.

To your question: if you parameter didn't parsed correctly you will have value of 0 in the execute() method. So, you decide what result to return. You can also check the value in the validate() method if the validation interceptor placed after params interceptor do parse or GenericValidator.isInt after that setFieldError() and show it in the JSP or redirect right after validation.

这篇关于Struts 2请求参数验证(整数和字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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