即使没有实现POST方法,Post方法上的Spring重定向也是如此

时间:2014-10-25 14:13:25

标签: java spring jsp spring-mvc

我是春天的新人,

我使用以下控制器创建了新的Spring MVC项目:

@Controller 公共类AccountController {

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



    WebUser webUser = new WebUser();
    model.addAttribute("user", webUser);
    return "RegistrationForm";
}

}

并使用以下JSP

    

注册

<form:form method="POST" action="user" modelAttribute ="user">
    <form:errors path="*" cssClass="errorblock" element="div" />
    <table>
        <tr>
            <td>First Name :</td>
            <td><form:input path="fName" />
            </td>
            <td><form:errors path="fName" cssClass="error" />
            </td>
        </tr>
            <tr>
            <td>Last Name :</td>
            <td><form:input path="lName" />
            </td>
            <td><form:errors path="lName" cssClass="error" />
            </td>
        </tr>
            <tr>
            <td>Middle Name :</td>
            <td><form:input path="mName" />
        </td>
            <td><form:errors path="mName" cssClass="error" />
            </td>
        </tr>
        <tr>
            <td>User Name :</td>
            <td><form:input path="username" />
            </td>
            <td><form:errors path="username" cssClass="error" />
            </td>
        </tr>
        <tr>
            <td>Password :</td>
            <td><form:input path="password" type="password"/>
            </td>
            <td><form:errors path="password" cssClass="error" />
            </td>
        </tr>
        <tr>
            <td>Password Confirmation:</td>
            <td><form:input path="confPassword"  type="password" />
            </td>
            <td><form:errors path="confPassword" cssClass="error" />
            </td>
        </tr>
        <tr>
            <td>Email Address:</td>
            <td><form:input path="email" />
            </td>
            <td><form:errors path="email" cssClass="error" />
            </td>
        </tr>
        <tr>
        <tr>        
            <td>Address :</td>
        </tr>
        <tr>        
            <td>Country :</td>
            <td><form:input path="country" />
            </td>
            <td><form:errors path="country" cssClass="error" />
            </td>
        </tr>
        <tr>
            <td>State :</td>
            <td><form:input path="state" />
            </td>
            <td><form:errors path="state" cssClass="error" />
            </td>
        </tr>
        <tr>
            <td>City :</td>
            <td><form:input path="city" />
            </td>
            <td><form:errors path="city" cssClass="error" />
            </td>
        </tr>
        <tr>
            <td>Street :</td>
            <td><form:input path="street" />
            </td>
            <td><form:errors path="street" cssClass="error" />
            </td>
        </tr>


        <tr>
            <td>Year of Graduation :</td>
            <td><form:input path="yOfGrad" />
            </td>
            <td><form:errors path="yOfGrad" cssClass="error" />
            </td>
        </tr>
    <tr>
            <td colspan="3"><input type="submit" value="Submit"/></td>
        </tr>
    </table>
</form:form>

所以每次点击提交时,应用程序都会将我重定向到/ User,即使我从未在应用程序的任何位置指定它。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

表单中的“操作”属性:表单标记定义了您在单击提交按钮时指向的网址,因为您输入了'action =“user”'您将被定向到'/ user'。我假设你要直接想要'/ register'之类的东西,所以你应该设置'action =“register”'。