<bdo id='omubT'></bdo><ul id='omubT'></ul>
      <tfoot id='omubT'></tfoot>

        <small id='omubT'></small><noframes id='omubT'>

        <i id='omubT'><tr id='omubT'><dt id='omubT'><q id='omubT'><span id='omubT'><b id='omubT'><form id='omubT'><ins id='omubT'></ins><ul id='omubT'></ul><sub id='omubT'></sub></form><legend id='omubT'></legend><bdo id='omubT'><pre id='omubT'><center id='omubT'></center></pre></bdo></b><th id='omubT'></th></span></q></dt></tr></i><div id='omubT'><tfoot id='omubT'></tfoot><dl id='omubT'><fieldset id='omubT'></fieldset></dl></div>
      1. <legend id='omubT'><style id='omubT'><dir id='omubT'><q id='omubT'></q></dir></style></legend>
      2. Struts 2 请求参数验证(Int 和字符串)

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

        <legend id='w8QU3'><style id='w8QU3'><dir id='w8QU3'><q id='w8QU3'></q></dir></style></legend>

        • <tfoot id='w8QU3'></tfoot>

          <small id='w8QU3'></small><noframes id='w8QU3'>

              <tbody id='w8QU3'></tbody>
              • <bdo id='w8QU3'></bdo><ul id='w8QU3'></ul>

                <i id='w8QU3'><tr id='w8QU3'><dt id='w8QU3'><q id='w8QU3'><span id='w8QU3'><b id='w8QU3'><form id='w8QU3'><ins id='w8QU3'></ins><ul id='w8QU3'></ul><sub id='w8QU3'></sub></form><legend id='w8QU3'></legend><bdo id='w8QU3'><pre id='w8QU3'><center id='w8QU3'></center></pre></bdo></b><th id='w8QU3'></th></span></q></dt></tr></i><div id='w8QU3'><tfoot id='w8QU3'></tfoot><dl id='w8QU3'><fieldset id='w8QU3'></fieldset></dl></div>
                  本文介绍了Struts 2 请求参数验证(Int 和字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我在这里有这个网址

                  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.

                  推荐答案

                  您有一个参数字段为 int,但您已在 URL 中将其设置为 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){}
                  } 
                  

                  就是这样,您不再有任何例外,并且在 setter 中检查了 String 类型的参数.

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

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

                  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 请求参数验证(Int 和字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Compiling C++ for the JVM(为 JVM 编译 C++)
                  Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                  How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                  Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                  How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                  Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)
                    <tbody id='Lqx0e'></tbody>
                    <bdo id='Lqx0e'></bdo><ul id='Lqx0e'></ul>

                      <tfoot id='Lqx0e'></tfoot>

                          <small id='Lqx0e'></small><noframes id='Lqx0e'>

                        1. <i id='Lqx0e'><tr id='Lqx0e'><dt id='Lqx0e'><q id='Lqx0e'><span id='Lqx0e'><b id='Lqx0e'><form id='Lqx0e'><ins id='Lqx0e'></ins><ul id='Lqx0e'></ul><sub id='Lqx0e'></sub></form><legend id='Lqx0e'></legend><bdo id='Lqx0e'><pre id='Lqx0e'><center id='Lqx0e'></center></pre></bdo></b><th id='Lqx0e'></th></span></q></dt></tr></i><div id='Lqx0e'><tfoot id='Lqx0e'></tfoot><dl id='Lqx0e'><fieldset id='Lqx0e'></fieldset></dl></div>
                          <legend id='Lqx0e'><style id='Lqx0e'><dir id='Lqx0e'><q id='Lqx0e'></q></dir></style></legend>