如何将 JSP <form> 中的值传递给 java 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13635207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:27:43  来源:igfitidea点击:

how to pass values in JSP <form> into a java method

javahtmljspjakarta-eeservlets

提问by kaustav datta

I have a a JSP file in this format(two select tags)-

我有一个这种格式的 JSP 文件(两个选择标签)-

<%@ page import="mypackage.*" %>

<all the main tags>...

<form> 
<select> options... </select> 
<select> options... </select> 
<input type="submit" value="submit" /> 
</form>

<textarea></textarea>

There is a java method inside "mypackage" which should take arguments from the <select>tags after clicking on submit. The method returns a string which I want to output in the <textarea>. How do I go about this ?

“mypackage”中有一个java方法,它应该<select>在点击提交后从标签中获取参数。该方法返回一个字符串,我想在<textarea>. 我该怎么做?

Thanks a lot guys.

非常感谢各位。

回答by Eng.Fouad

Send it as HTTP POSTor HTTP GETto a servlet, and receive it via doGet()or doPost()methods. You can access it via HttpServletRequest.getParameter().

将它作为HTTP POST或发送HTTP GET到 servlet,并通过doGet()doPost()方法接收它。您可以通过 访问它HttpServletRequest.getParameter()

void doPost(HttpServletRequest request, HttpServletResponse response)

回答by Robert

I see that you are importing the mypackage.* classes into your JSP. Indeed, you could include Java code inside your JSP and call the class directly. Something like:

我看到您正在将 mypackage.* 类导入 JSP。实际上,您可以在 JSP 中包含 Java 代码并直接调用该类。就像是:

<%
    MyClass c = new MyClass();
    String result = c.doSomething(request.getParameter("select"));
    out.println("<textarea>" + result + "</textarea>");
%>

should be sufficient (but not good: the result should be escaped).

应该足够了(但不好:结果应该被转义)。

However, this code is not very maintainable and it can be done better (the answer of kaustav datta is one standard way of doing it).

然而,这段代码不是很容易维护,它可以做得更好(kaustav datta 的答案是一种标准的做法)。

It can be done in a more elegant way using the Spring framework's MVC part: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html

它可以使用 Spring 框架的 MVC 部分以更优雅的方式完成:http: //static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html

It needs some configuration at the beginning and takes some time to understand, but when you got it, it is very nice.

一开始需要一些配置,需要一些时间来理解,但是当你得到它时,它是非常好的。

In your case, a controller of the following form would be sufficient:

在您的情况下,以下形式的控制器就足够了:

@Controller
public class SelectController {

   private final class MyClass c = new MyClass();

   @RequestMapping(value="/select", method = RequestMethod.POST)
   public String doSelect(@RequestParam("selection") final String selection, final ModelMap model) {
       final String result = c.doSomething(selection);
       modelMap.addAttribute("result", result);

       return "yourJsp";
   }
} 

回答by Dedaniya HirenKumar

request.getParameterValues("select")

request.getParameterValues("select")

here getParameterValuesis a method of the request interface which returns a string in argument of this method pass name of your controller

getParameterValues是请求接口的一个方法,它在控制器的方法传递名称的参数中返回一个字符串

when you get value from text box use the method request.getParameter("name");

当您从文本框中获取值时,请使用该方法 request.getParameter("name");