从请求 URL 中检索值

发布于 2024-09-17 08:32:05 字数 148 浏览 3 评论 0原文

我有一个 URL,它将调用我的 Java 方法。它看起来像这样:

http://www.example.com/method?value=24

如何检索值 24 并在我调用的方法中使用它?

I have an URL which will call a method my method in Java. It looks like this:

http://www.example.com/method?value=24

How can I retrieve value 24 and use it in my called method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

╰つ倒转 2024-09-24 08:32:05

好的,在 wicket 中访问请求参数的方法是这样的:

final Request request = RequestCycle.get().getRequest();
final String valueAsString = request.getParameter("value");
int value = Integer.parseInt(valueAsString);

但通常你不必这样做,因为你通过使用 BookmarkablePageLinksPageParameters 对象并从 页面构造函数。阅读本页,了解有关 Wicket 中可添加书签的链接的一些材料

OK, the way to access a request parameter in wicket is this:

final Request request = RequestCycle.get().getRequest();
final String valueAsString = request.getParameter("value");
int value = Integer.parseInt(valueAsString);

But usually you don't have to do that, because you pass parameters by using BookmarkablePageLinks with PageParameters objects and read those objects from the page constructors. Read this page for some material on Bookmarkable Links in Wicket.

你另情深 2024-09-24 08:32:05

我在这里假设您确实在 Java 代码中创建了数字“24”,因为您说您正在使用 Wicket。

因此,正如 Seanizer 已经说过的,在 99% 的情况下,您不需要解析请求 url 来获取值,这样的东西应该足够了:

public class MyPage extends WebPage {
   public MyPage() {
      // your number is generated somehow
      final int value = 24;

      this.add(new Link<Integer>("myLink") {
         @Override
         public void onClick() {
            // do something with the value
            int newValue = value * 2;
         }
      }
   }
}

或者 - 对于模型 - 像这样

public class MyPage extends WebPage {
       public MyPage() {
          // your number is generated somehow
          int value = 24;

          Model<Integer> model = new Model<Integer>(value);

          this.add(new Link<Integer>("myLink", model) {
             @Override
             public void onClick() {
                // your '24'
                Integer value = this.getModelObject();
                // do something with the value
                int newValue = value * 2;
             }
          }
       }
    }

如果你真的,真的,真的 -确实需要 URL 中的参数,我想这就是您想要的:

public class MyPage extends WebPage {
       public MyPage(PageParameters parameters) {
          // your number is generated somehow
          Integer value = parameters.getAsInteger("value");
       }
    }

根据您配置应用程序的方式,您可能需要相应地实现其他构造函数。

祝你好运。

I am assuming here that you do create the number - the '24' - in your Java code, since you say you are using Wicket.

Thus, as seanizer already said, in 99% of the cases, you do not need to parse the request url to get the value, something like this should be sufficient:

public class MyPage extends WebPage {
   public MyPage() {
      // your number is generated somehow
      final int value = 24;

      this.add(new Link<Integer>("myLink") {
         @Override
         public void onClick() {
            // do something with the value
            int newValue = value * 2;
         }
      }
   }
}

or - with models - like this

public class MyPage extends WebPage {
       public MyPage() {
          // your number is generated somehow
          int value = 24;

          Model<Integer> model = new Model<Integer>(value);

          this.add(new Link<Integer>("myLink", model) {
             @Override
             public void onClick() {
                // your '24'
                Integer value = this.getModelObject();
                // do something with the value
                int newValue = value * 2;
             }
          }
       }
    }

If you really, really, REALLY-REALLY do need the parameter from the URL, I guess this is what you want:

public class MyPage extends WebPage {
       public MyPage(PageParameters parameters) {
          // your number is generated somehow
          Integer value = parameters.getAsInteger("value");
       }
    }

Depending on how you configured your application, you might need to implement the other constructors accordingly.

Good luck.

叫嚣ゝ 2024-09-24 08:32:05

如果您使用的是 Servlet,则可以使用

ServletRequest.getParameter(...)< /a>

如果使用 JSP,则可以使用 request 隐式对象

request.getParameter(...)

If you are using Servlets, you can use

ServletRequest.getParameter(...)

If you use JSP, you can use the request implicit object

request.getParameter(...)

时光磨忆 2024-09-24 08:32:05

像这样的事情:

public class ExampleServlet extends HttpServlet {

   /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
   protected void service(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

       String s = request.getParameter("value");
       int value = 0;
       try {
            value = Integer.parseInt(s);
       } catch(NumberFormatException nfe) {
            nfe.printStackTrace();
       }
   }
}

Something like this:

public class ExampleServlet extends HttpServlet {

   /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
   protected void service(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

       String s = request.getParameter("value");
       int value = 0;
       try {
            value = Integer.parseInt(s);
       } catch(NumberFormatException nfe) {
            nfe.printStackTrace();
       }
   }
}
樱桃奶球 2024-09-24 08:32:05

其中之一:

  1. 在可添加书签的页面中,创建具有单个参数 PageParameters 的构造函数并从那里获取值: p.getString("value") 或 p.getInt("value)
  2. 也是页面级别的,但如果您没有该构造函数或例如在组件中: getPage().getPageParameters() 并从
  3. 任何组件中获取它, getRequest().getParameter("value") (或使用 Request.get() 代替)

还有更多获取它的方法,但这些是最直接的。

One of:

  1. in a bookmarkable page, create constructor with single argument PageParameters and get the value from there: p.getString("value") or p.getInt("value)
  2. also page level but if you don't have that constructor or are e.g. in a component: getPage().getPageParameters() and get it from there
  3. from any component, getRequest().getParameter("value") (or use Request.get() instead)

There are even more ways of getting it, but these are the most straightforward.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文