Form controller Simpleformcontroller with spring MVC

Source: Internet
Author: User
Tags object object xmlns

Take the registration process as an example, we may choose to inherit abstractcontroller to implement the form display, inherit Abstractcommandcontroller to implement the form processing, this is feasible, but must maintain two controllers

In this case, we should use Simpleformcontroller, when he accepts a GET request, displays the form, processes the form when it accepts a POST request, and if an error occurs, the controller will know to display the form again so that the user can modify the error and resubmit

The Pojo of the form corresponding to

package model;

public class Student ...{
  private String name;
  private String sex;
public String getName() ...{
   return name;
}
public void setName(String name) ...{
   this.name = name;
}
public String getSex() ...{
   return sex;
}
public void setSex(String sex) ...{
   this.sex = sex;
}
}

Controller:

There is also a dosubmitaction () method in this base class, and the OnSubmit () method is the difference between the latter can return a Modelandview object, the ability to output data to the page, while the former can not return data to the page, Both methods have at the same time only one valid

Package Action;

import model.Student;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class RegisterStudentController extends SimpleFormController ...{

   public RegisterStudentController()...{
     this.setCommandClass(Student.class);
   }

   protected ModelAndView onSubmit(Object object, BindException arg1) throws Exception ...{
     Student stu=(Student)object;
     return new ModelAndView(getSuccessView(),"student",stu);
   }

}

Xml

<?xml version= "1.0" encoding= "UTF-8"
<web-app version= "2.4"
xmlns= "http://java.sun.com/" Xml/ns/j2ee "
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "
xsi:schemalocation=" http:// JAVA.SUN.COM/XML/NS/J2EE
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd
<context-param>
& Lt;param-name>contextconfiglocation</param-name>
<param-value>/web-inf/train-service.xml,/ Web-inf/train-data.xml,/web-inf/train-servlet.xml</param-value>
</context-param>
<servlet
<servlet-name>train</servlet-name>
<servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class>
<load-on-startup>0</ Load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>train</ Servlet-name>
<url-pattern>*.mvc</url-pattern>
</servlet-mapping>
<filter>
<filter-name>character</filter-name>
<filter-class>action.characterfilter</ Filter-class>
</filter>
<filter-mapping>
<filter-name>character</filter-name& Gt
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
; Welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.