在request.getParameter(“something”)中获取null

时间:2015-08-13 22:50:30

标签: java spring jsp

我的应用程序(Spring,JSP)无法从下拉框中读取值,总是将null作为参数。

JSP:

<select>
<c:forEach items="${countries}" var="country">
<option value = "$country.id_country">${country.name}</option>
</c:forEach>
</select>

控制器:

@RequestMapping(value="add", method=RequestMethod.GET)
        public ModelAndView addOrganization() {
        ModelAndView modelAndView = new ModelAndView("add");
        Organization organization = new Organization();
        modelAndView.addObject("organization", organization);
        List<Country> countries = countryService.listOfCountries();
        modelAndView.addObject("countries", countries);
        return modelAndView;
    }

    @RequestMapping(value="add", method=RequestMethod.POST)
    public ModelAndView addingConfirm(Organization organization, HttpServletRequest request)
{
        ModelAndView modelAndView = new ModelAndView("confirm");
            Integer id_country = null;
        if (request.getParameter("сountry") != null
                && !request.getParameter("сountry").equals("")) {
            id_country = Integer.parseInt(request.getParameter("сountry"));
            organization.setCountry(id_country);
        }
        organizationService.addOrganization(organization);
        String message = "Organization was successfully added.";
        modelAndView.addObject("message", message);
        return modelAndView;
    }

因此,参数country始终为null

1 个答案:

答案 0 :(得分:1)

如果没有select属性,name将不会发布。

此外,value上的<option>属性缺少一些大括号{}。

你应该始终(!)确保通过将${country.name}包裹在<c:out>中来逃避您生成的任何动态文本。

<select name="country">
  <c:forEach items="${countries}" var="country">
    <option value="${country.id_country}"><c:out value="${country.name}"/></option>
  </c:forEach>
</select>

如果id_country是一个字符串,您甚至应该考虑将其包装在<c:out>中。 id_countryname的包装是可选的,如果您绝对(!)确信可以信任它们不包含跨站点脚本攻击,但是养成逃避习惯是个好主意所有文字值。