简体   繁体   English

JSP使用Spring MVC获取/发布

[英]JSP get/post with Spring MVC

I'm trying to create a registration form. 我正在尝试创建一个注册表单。 In this form there are some fields(username,password etc) and also a dropdown listbox. 在此表单中有一些字段(用户名,密码等)以及下拉列表框。 My problem is with this listbox.I manage to retrieve succesfully from a server all the items that will be added in the listbox and i want know to put it in this form. 我的问题是这个listbox。我设法从服务器成功检索将添加到列表框中的所有项目,我想知道将它放在这个表单中。

More specific i have a list called lista, which contains the items i want to add in the listbox. 更具体地说,我有一个名为lista的列表,其中包含我要在列表框中添加的项目。

JSP: JSP:

<c:url var="attinto" value="/reg" />
<form:form modelAttribute="attribute" method="POST" action="${attinto}">
<table>
    .
            .

    <tr>
        <td><form:label path="researchareas">Research area:</form:label></td>
        <td>    
          <select multiple="multiple">   
            <c:forEach items="${researchareas}" var="researcharea">     
              <option value="${researcharea}">  ${researcharea} </option>                   
            </c:forEach>
          </select>    
        </td> 
    </tr>

Controller: 控制器:

    @RequestMapping(value = "/reg", method = RequestMethod.GET)
    public String getform(Model model) {

        getAll(model);
        model.addAttribute("attribute", new Reg());

        return "reg";
    }

I have to mention at this point that the getAll(model) is a void method similar to the following: 我必须在这一点上提到getAll(model)是一个类似于以下的void方法:

      model.addAttribute("researchareas",lista);

Then i create a POST method to send this data. 然后我创建一个POST方法来发送这些数据。

Question:How can i add in the form the data from the list(into a listbox)?How i can take back the values that the user will select? 问题:如何在表单中添加列表中的数据(进入列表框)?我如何收回用户将选择的值?

First of all, use form:select like this: 首先,使用form:select如下:

<form:select path="researchareas" items="${researchareas}" multiple="true" />

Then Spring could automatically bind corresponding attribute in Registration object: 然后Spring可以自动绑定Registration对象中的相应属性:

@RequestMapping(value = "/registration", method = RequestMethod.POST)
public String getRegistrationForm( //
    @ModelAttribute("registrationAttribute") Registration registration, //
    BindingResult result, Model model) {
  ...
  return ...
}

Assuming Registration class has the following: 假设注册课程具有以下内容:

public class Registration {
  String username;
  String password;
  List<String> researchareas;

  ... corresponding getters and setters here
}

Though I'd name attribute the same as class or else you'd have to specify explicit names in the method parameters annotations. 虽然我将name属性命名为class,否则你必须在方法参数注释中指定显式名称。

Please specify post method in your form in jsp and also in controller specify method=RequestMethod.POST . 请在jsp中指定表单中的post方法,并在控制器中指定method = RequestMethod.POST

One more thing. 还有一件事。

There should not be the list as your data type for researchareas in your Registration class. 在您的注册类中,不应该将列表作为研究员的数据类型。 Try giving the datatype as String[] 尝试将数据类型设为String []

Hope this works for you. 希望这对你有用。 Cheers. 干杯。

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

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