[JBDJ] Springmvc FRAME (4) Controller controllers

Source: Internet
Author: User

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

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.