无法在SpringMVC上提交表单

时间:2015-03-16 01:13:15

标签: spring spring-mvc twitter-bootstrap-3

我是SpringMVC的新手,并且有一个无法提交到后端的表单。我创建了如下表单,当我提交时,将返回错误404。我将操作更改为/ MyProject / contact但无效。

<form class="form-horizontal" role="form" method="post"
                action="/contact">

                <div class="form-group">
                    <div class="col-md-12">
                        <label class="sr-only" for="exampleInputName2">Name 
                        </label> <input type="text" class="form-control" id="name"
                            name="name" placeholder="Your name" value="">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-12">
                        <label class="sr-only" for="exampleInputName2">Email
                            Address</label> <input type="email" class="form-control" id="email"
                            name="email" placeholder="Your email" value="">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-12">
                        <label class="sr-only" for="exampleInputName2">Phone
                            Number</label> <input type="number" class="form-control" id="phone"
                            name="phone" placeholder="Phone number" value="">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-12">
                        <label class="sr-only" for="exampleInputName2">Enquiry</label>
                        <textarea class="form-control" rows="4" name="message"
                            placeholder="Please enter your enquiry"></textarea>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-2 " style="float: right;">
                        <input id="submit" name="submit" type="submit" value="Send"
                            class="btn btn-primary">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-10 col-sm-offset-2">
                        <! Will be used to display an alert to the user>
                    </div>
                </div>
            </form>

控制器

@Controller
public class ContactController {

    @RequestMapping(value="/contact", method=RequestMethod.POST)
    public String processForm(Contact contact, Model model){
        System.err.println("Contact Name is:" + contact.getName());
        return null;
    }
}

错误

HTTP Status 404 - /contact

type Status report

message /contact

description The requested resource is not available.

2 个答案:

答案 0 :(得分:1)

它的beacuse spring不知道如何将参数联系联系传递给您的控制器方法。你需要做几件事才能让它发挥作用。将表单更改为如下所示。

<form class="form-horizontal" role="form" method="post" modelAttribute="contact" action="/contact">

您的控制器将联系人作为模型属性。

@Controller
public class ContactController {

    @RequestMapping(value="/contact", method=RequestMethod.POST)
    public String processForm(@ModelAttribute Contact contact, Model model){
        System.err.println("Contact Name is:" + contact.getName());
        return null;
    }
}

为了更好地理解模型属性的作用,网上有大量的样本和解释。希望这会有所帮助。

答案 1 :(得分:0)

我可以在minion's answer的帮助下解决问题,跟随this tutorial并添加以下链接

    @RequestMapping(value = "/contact", method = RequestMethod.GET)
    public ModelAndView contactForm() {
        System.err.println("here in GET");
        return new ModelAndView("contact", "command", new Contact());
    }