简体   繁体   English

如何在JSP中传递值 <form> 到一个java方法

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

I have aa 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. 单击“提交”后,“ mypackage”中有一个java方法,该方法应从<select>标记中获取参数。 The method returns a string which I want to output in the <textarea> . 该方法返回我要在<textarea>输出的字符串。 How do I go about this ? 我该怎么办?

Thanks a lot guys. 非常感谢。

Send it as HTTP POST or HTTP GET to a servlet, and receive it via doGet() or doPost() methods. 将其作为HTTP POSTHTTP GET发送到servlet,并通过doGet()doPost()方法接收它。 You can access it via HttpServletRequest.getParameter() . 您可以通过HttpServletRequest.getParameter()访问它。

void doPost(HttpServletRequest request, HttpServletResponse response)

I see that you are importing the mypackage.* classes into your JSP. 我看到您正在将mypackage。*类导入到JSP中。 Indeed, you could include Java code inside your JSP and call the class directly. 确实,您可以在JSP中包含Java代码并直接调用该类。 Something like: 就像是:

<%
    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";
   }
} 

request.getParameterValues("select")

here getParameterValues is 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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM