Controller: What class the Action inherits.
1}jsp->jsp Direct Forwarding
Org.springframework.web.servlet.mvc.ParameterizableViewController
Springmvc.xml
<!-- /index.action request, forward directly to/index.jsp page --> < bean name ="/index.action " class = " Org.springframework.web.servlet.mvc.ParameterizableViewController " > < property name =" ViewName " value Span style= "COLOR: #0000ff" >= "/index.jsp" /> </ bean >
2} collect parameters in entity form
Org.springframework.web.servlet.mvc.AbstractCommandController
Jsp
<formAction= "${pagecontext.request.contextpath}/back.action"Method= "POST">User name:<inputtype= "text"name= "username" /><P>Password:<inputtype= "Password"name= "Userpewd" /><P>Age:<inputtype= "text"name= "Age" /><P>Date of entry:<inputtype= "text"name= "Userdate" /><P><inputtype= "Submit"value= "Submit" /></form>
Springmvc.xml
< = "/back.action" = "beanaction" > </ Bean > <!-- --<Beanclass= " Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping "></Bean >
Beanaction.java
ImportJava.text.SimpleDateFormat;Importjava.util.Date;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.beans.propertyeditors.CustomDateEditor;Importorg.springframework.validation.BindException;ImportOrg.springframework.web.bind.ServletRequestDataBinder;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.mvc.AbstractCommandController;/** * @authorAdministrator **/@SuppressWarnings ("Deprecation") Public classBeanactionextendsabstractcommandcontroller{//Null parameter constructor, injecting user object Publicbeanaction () { This. Setcommandclass (User.class);}/*** Custom type converter, will string->date type (format yyyy-mm-dd)*/@Overrideprotected voidInitbinder (httpservletrequest request,servletrequestdatabinder Binder)throwsException {//inject a custom type converter inside the Springmvc//parameter one: What type of bytecode the string is converted to//parameter two: custom conversion rules//true to indicate that the date field can be emptybinder.registercustomeditor (Date.class,NewCustomdateeditor (NewSimpleDateFormat ("Yyyy-mm-dd"),true));} @OverrideprotectedModelandview handle (httpservletrequest request,httpservletresponse response, Object obj, bindexception error) throwsException {//Building ObjectsModelandview Andview=NewModelandview (); User User=NULL; if(objinstanceofUser) {User=(User) obj; } System.out.println (User.tostring ()); Andview.addobject ("Username", User.getusername ()); Andview.addobject ("Userpewd", User.getuserpewd ()); Andview.addobject ("Age", User.getage ()); Andview.addobject ("Userdate", User.getuserdate ()); Andview.setviewname ("/back.jsp");returnAndview;}}
User. java
Importjava.util.Date; Public classUser {PrivateString username;PrivateString userpewd;PrivateInteger age;PrivateDate userdate; PublicString GetUserName () {returnusername;} Public voidSetusername (String username) { This. Username =username;} PublicString getuserpewd () {returnuserpewd;} Public voidsetuserpewd (String userpewd) { This. userpewd =userpewd;} PublicInteger getage () {returnAge ;} Public voidsetage (Integer age) { This. Age =Age ;} PublicDate getuserdate () {returnuserdate;} Public voidsetuserdate (Date userdate) { This. userdate =Userdate;} @Override PublicString toString () {return"User [username=" + Username + ", userpwd=" + userpewd + ", age=" + Age + ", userdate=" + Userdate + "]";}}
back.jsp
<%@ Page Language="Java"Import="java.util.*"pageencoding="Utf-8"%><HTML> <Head> <title>My JSP ' index.jsp ' starting page</title> </Head> <Body>This is the test on page 2<BR>${username}<P>${userpewd}<P>${age}<P>${userdate}<P> </Body></HTML>
[JBDJ] Springmvc FRAME (4) Controller controllers