简体   繁体   中英

Controller doesn't work JAVA. Spring MVC

I have a problem with sending <form> to controller. When i'm pressing submit button it shows me 400 error. And when i'm trying to output smth into console in POST method it doesn't show it. Why controller doesn't get my form? Thanx! 400错误

Here is my JSP page

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://www.springframework.org/security/tags"
    prefix="sec"%>
<%@page language="java" session="true"%>
<%
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
%>
<!DOCTYPE html>
<fmt:requestEncoding value="utf-8" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Поиск оппонента | Haine and Vold</title>
<link rel="shortcut icon" href="<c:url value="/resources/faviicon/iconHV.ico"/>" type="image/x-icon">
<link href="<c:url value="/resources/bootstrap/bootstrap.css"/>"
    rel="stylesheet" type="text/css">
<link href="<c:url value="/resources/CSS/styles.css"/>" rel="stylesheet"
    type="text/css">

</head>
<body>
<div class="container">
        <img class="img-responsive logo" src="/app/resources/logo.jpg">
    </div>
    <h5 class="text-center noparamserror">${noparams}</h5>
    <c:url var="getSearch" value="/user/search" />
    <br>
    <div class="container text-center">
        <form:form class="form-inline centerBlock" modelAttribute="users"
            method="POST" action="/app/user/search" accept-charset="utf-8">

            <div class="form-group">
                <label class="control-label" for="weight"><spring:message
                        code="label.searchparamweight" /></label>
                <form:select multiple="" class="form-control input-sm" path="weight"
                    name="weight">
                    <form:option value=""></form:option>
                    <form:option value="40"></form:option>
                    <form:option value="50"></form:option>
                    <form:option value="60"></form:option>
                    <form:option value="70"></form:option>
                    <form:option value="80"></form:option>
                    <form:option value="90"></form:option>
                    <form:option value="100"></form:option>
                    <form:option value="110"></form:option>
                    <form:option value="120"></form:option>
                </form:select>
            </div>

            <div class="form-group">
                <label class="control-label" for="gender"><spring:message
                        code="label.gender" /></label>
                <form:select multiple="" class="form-control input-sm" name="gender"
                    path="gender">
                    <form:option value=""></form:option>
                    <form:option value="Мужской"></form:option>
                    <form:option value="Женский"></form:option>
                </form:select>
            </div>
            <div class="form-group">

                <label class="control-label" for="place"><spring:message
                        code="label.place" /></label>

                <form:select class="form-control input-small" name="place"
                    path="place">
                    <form:option value=""></form:option>
                    <form:option value="Дзержинский"></form:option>
                    <form:option value="Киевский"></form:option>
                    <form:option value="Комминтерновский"></form:option>
                    <form:option value="Лененский"></form:option>
                    <form:option value="Московский"></form:option>
                    <form:option value="Октябрский"></form:option>
                    <form:option value="Орджоникидзевский"></form:option>
                    <form:option value="Фрунзенский"></form:option>
                    <form:option value="Холодная гора"></form:option>
                    <form:option value="Червонозаводской"></form:option>
                </form:select>
            </div>

            <div class="form-group">

                <label class="control-label" for="age"><spring:message
                        code="label.searchparamage" /></label>

                <form:select multiple="" class="form-control input-small" name="age"
                    path="age">
                    <form:option value=""></form:option>
                    <form:option value="19"></form:option>
                    <form:option value="20"></form:option>
                    <form:option value="21"></form:option>
                    <form:option value="22"></form:option>
                    <form:option value="23"></form:option>
                    <form:option value="24"></form:option>
                    <form:option value="25"></form:option>
                    <form:option value="26"></form:option>
                    <form:option value="27"></form:option>
                    <form:option value="28"></form:option>
                    <form:option value="29"></form:option>
                    <form:option value="30"></form:option>
                    <form:option value="31"></form:option>
                    <form:option value="32"></form:option>
                    <form:option value="33"></form:option>
                    <form:option value="34"></form:option>
                    <form:option value="35"></form:option>
                    <form:option value="36"></form:option>
                    <form:option value="37"></form:option>
                    <form:option value="38"></form:option>
                    <form:option value="39"></form:option>
                    <form:option value="40"></form:option>
                    <form:option value="41"></form:option>
                    <form:option value="42"></form:option>
                    <form:option value="43"></form:option>
                    <form:option value="44"></form:option>
                    <form:option value="45"></form:option>
                </form:select>
            </div>


            <button class="btn btn-success" type="submit">
                <spring:message code="label.search" />
            </button>


        </form:form>

    </div>
    <br>
    <div class="scrollTableSearch">
        <c:if test="${!empty listUsersSort}">


            <table
                class="table table-striped table-bordered table-condensed table-hover">

                <tr>
                    <th><spring:message code="label.name" /></th>
                    <th><spring:message code="label.surname" /></th>
                    <th><spring:message code="label.gender" /></th>
                    <th><spring:message code="label.weight" /></th>
                    <th><spring:message code="label.height" /></th>
                    <th><spring:message code="label.sport" /></th>
                    <th><spring:message code="label.place" /></th>
                </tr>
                <c:forEach items="${listUsersSort}" var="users">
                    <tr>

                        <td><a href="<c:url value='/user/search/${users.username}'/>"
                            target="_blank"> ${users.name}</a></td>
                        <td>${users.surname}</td>
                        <td>${users.gender}</td>
                        <td>${users.weight}</td>
                        <td>${users.height}</td>
                        <td>${users.sport}</td>
                        <td>${users.place}</td>
                    </tr>
                </c:forEach>
            </table>

        </c:if>
    </div>
</body>
<script src="<c:url value="/resources/Jquery/jquery-2.1.4.min.js"/>"
    type="text/javascript"></script>
<script src="<c:url value="/resources/bootstrap/bootstrap.js"/>"
    type="text/javascript"></script>

</html>

Here is my controller method.

    @RequestMapping(value = "/user/search", method = RequestMethod.POST,produces = "text/plain;charset=UTF-8")
    public String listUsersSort(
            @RequestParam(value="weight") int weight,
            @RequestParam(value="gender") String gender,
            @RequestParam(value="place") String place,
            @RequestParam(value="age") int age,
            Model model, Principal principal)
    {

        System.out.println("int");
        if(weight<40 && gender.equals("") && place.equals("") && age<19)
        {
            model.addAttribute("users", new Users());
model.addAttribute("noparams", "Задайте параметры поиска");
return "searchingUser";
        }

        else{
    model.addAttribute("users", new Users());
        model.addAttribute("listUsersSort", usersService.listUsersSort(weight, gender, place, age, principal.getName()));
        model.addAttribute("allUserMess", this.dialogService.allNewMessForUser(principal.getName()));
        model.addAttribute("countOfFeedbacks", this.feedbackService.checkUnreadFedbacks().size());
        model.addAttribute("countOfNewUsers", this.usersService.countOfNewUsers());
        return "searchingUser";
        }}

You're getting the error because your code is somewhere in between using the form-backing bean, or binding the parameters from request. You should either remove the modelAttribute and path attributes from your form.

Or remove the @RequestParam annotated arguments from your request, and create a form-backing object with weight , age , place , gender properties, and add the object to the controller method signature annotated with @ModelAttribute("users")

In your JSP page, actually you are missing the Context Root while giving request to your controller.

<form:form class="form-inline centerBlock" modelAttribute="users"
            method="POST" action="/app/user/search" accept-charset="utf-8">

In this case the request url will be :

http://yourHost:portNo/app/user/search

So to solve this problem you can include your context root with the action:

<form:form class="form-inline centerBlock" modelAttribute="users"
                method="POST" action="/your-context-root/app/user/search" accept-charset="utf-8">

OR by writing in this way:

<spring:url value="/app/user/search" var="formUrl"/>
<form:form class="form-inline centerBlock" modelAttribute="users"
                    method="POST" action="${formUrl}" accept-charset="utf-8">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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