简体   繁体   English

验证后重新提交表单的问题

[英]Issue with resubmitting the form after validation

I am trying to learn Spring MVC with small application but got stuck with an issue. 我试图通过小型应用程序学习Spring MVC,但遇到了问题。

I have created a form for user registration, now on form submission I am doing validation using annotations. 我已经创建了一个用于用户注册的表单,现在在表单提交时,我正在使用批注进行验证。

When the validation fails, I am re-displaying the form with error details. 验证失败时,我将重新显示带有错误详细信息的表单。 But when I re-submit the form, I am facing issue with how to handle the URL mapping. 但是,当我重新提交表单时,我面临着如何处理URL映射的问题。

Here is my controller code: 这是我的控制器代码:

@Controller
public class PersonController {

    private PersonService personService;

    public PersonService getPersonService() {
        return personService;
    }

    @Autowired
    public void setPersonService(PersonService personService) {
        this.personService = personService;
    }

    @RequestMapping("/allpersons")
    public String showPersonListPage(Map<String, Object> model) {
        model.put("persons", getPersonService().fetchAllPersons());
        return "person_list";
    }

    @RequestMapping(value = "/person", method = GET)
    public String showNewPersonForm(Map<String, Object> model) {
        model.put("person", new Person());
        return "new_person";
    } 

    @RequestMapping(value = "/person/add", method = RequestMethod.POST)
    public String addPersonFromForm(@Valid Person person,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return "new_person";
        }
        getPersonService().addPerson(person);
        return "redirect:/allpersons";
    }

}   

Initially I am trying to display the form using the URL as http://localhost:8080/myapp/person . 最初,我尝试使用URL作为http://localhost:8080/myapp/person显示表单。 The code for my form is like this: 我表单的代码如下:

new_person.jsp new_person.jsp

<div>
    <h2>New Person Form</h2>

    <sf:form method="POST" modelAttribute="person"
        action="person/add">
        <fieldset>
            <table cellspacing="0">
                <tr>
                    <th><label for="name">Name:</label></th>
                    <td><sf:input path="name" id="name" /></td>
                    <td><sf:errors path="name" /></td>
                </tr>
                <tr>
                    <th><label for="age">Age:</label></th>
                    <td><sf:input path="age" id="age" /></td>
                    <td><sf:errors path="age" /></td>
                </tr>
                <tr>
                    <th><input type="Submit" value="Submit" /></th>
                    <td></td>
                </tr>
            </table>
        </fieldset>
    </sf:form>

</div>

Now on form submission the URL pattern for my form is person/add so the URL is changing to http://localhost:8080/myapp/person/add . 现在在提交表单时,我表单的URL模式为person/add因此URL更改为http://localhost:8080/myapp/person/add Initially I am submitting the form with wrong information so the form is re-loaded with validation error messages. 最初,我提交的表单中包含错误的信息,因此该表单会重新加载验证错误消息。

Now when I re-submit the form with correct values, the URL is going to http://localhost:8080/myapp/person/person/add instead of http://localhost:8080/myapp/person/add . 现在,当我重新提交具有正确值的表单时,URL将转到http://localhost:8080/myapp/person/person/add而不是http://localhost:8080/myapp/person/add

Can someone please help me how to fix this basic issue? 有人可以帮我解决这个基本问题吗?

Let me know if you need any more details. 让我知道是否需要更多详细信息。

Here is my spring configuration file. 这是我的弹簧配置文件。

adding only main entries of the file: 仅添加文件的主要条目:

<mvc:annotation-driven validator="validator" />
    <context:component-scan
        base-package="com.examples" />

    <bean id="validator"
        class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix" value=".jsp" />
    </bean>

The PersonService (has @Service annotation) & Person are simple classes PersonService (具有@Service批注)和Person是简单的类

Maintain a single URL pattern for GET as well as POST . GETPOST维护一个URL模式。

So in your controller on form submission the handler should be like this: 因此,在表单提交控制器中,处理程序应如下所示:

@RequestMapping(value = "/person", method = RequestMethod.POST)
public String addPersonFromForm(@Valid Person person,
        BindingResult bindingResult) {
        ... // your code
}

In above code I have changed the URL pattern. 在上面的代码中,我更改了URL模式。

Now coming to your JSP, remove the action attribute to avoid the URL mismatch. 现在进入您的JSP,删除action属性以避免URL不匹配。

Making these 2 changes will fix the issue. 进行这两个更改将解决此问题。

I see no "form2" reference anywhere. 我在任何地方都看不到“ form2”引用。 Maybe you missed something at your question? 也许您遗漏了一些问题? Or just a mistake writing? 还是只是写错了?

Also, as an advice, I suggest you use the same URL using GET and POST methods, as it's a good practice. 另外,作为建议,我建议您使用GET和POST方法使用相同的URL,因为这是一个好习惯。

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

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