SPRINGMVC Annotation Development

Source: Internet
Author: User

First create a class in a package, and then configure Sprringmvacontroller.xml and link to the class.

<contextbase-package= "Cn.happy.controllerreturn"></Context  : Component-scan>
Annotations The classes created by development and their methods

Wildcard: is a symbol, not an exact match, but a substitute for

* * represents any level of directory, or no directory

 PackageCn.happy.Controler;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.PathVariable;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;/*** Created by Java on 2017/8/18. */@Controller @requestmapping ("/user")Public classHappycontroller {@RequestMapping ("/first") PublicString Dofirst () {return"/web-inf/index.jsp"; } @RequestMapping ("/second") PublicString Dosecond () {return"/web-inf/insert.jsp"; } @RequestMapping ("/*thtird") PublicString Dothird () {return"/web-inf/delete.jsp"; } @RequestMapping ("/**/fourth") PublicString Dofourth () {return"/web-inf/insert.jsp"; } @RequestMapping ("/*/six") PublicString Dosix () {return"/web-inf/insert.jsp"; The}//Path variable @RequestMapping (value ="/{name}/{age}/first.do", method = {Requestmethod.POST, Requestmethod.GET}) PublicString Dofirstcoller (@PathVariable () string name, @PathVariable () string age) {System. out. println (name); System. out. println (age);return"/web-inf/index.jsp"; } @RequestMapping (value ="/first.do", method = {Requestmethod.POST, Requestmethod.GET}) PublicString Dofirst (string name, model model) {System. out. println ("Name"+name); Model.addattribute ("Name"+name);return"Index"; }}
   Definition of the mode in the request:
   The value of the method property is Requestmethod, which is an enumeration constant
   First create a UserInfo entity class and write the attributes and encapsulate them:   
   Domain properties: The type of a variable in one class is another custom type.
UserInfo {    name;    Private   Book;     books;     list<book> getbooks () {        books;    }     setbooks (list<book> Books) {   //collection Properties this        .  = books;    }     Book GetBook () {        book;    }     setbook (book book) {This        .  = Book;    }     String getName () {        name;    }     setName (String name) {This        .  = name;}    }
And then create the book's entity class, also write a property to encapsulate it
Book {    bookname;     String getbookname () {        bookname;    }     setbookname (String bookname) {This        .  = BookName;    }}
Then create the Argementcontroller class
 @Controller  public class  Argementcontroller {@RequestMapping ( "/login2" )//qualification Request method  Public  String login2 (UserInfo info) {System.   out  .        println (Info.getname ());  return          "/web-inf/delete.jsp" ; } @RequestMapping ( "/arearggement" )  public  String login4 (UserInfo info) {Sy Stem.   out  . println (Info.getname () +  "  \ t  "  +        Info.getbook (). Getbookname ());  return      "/web-inf/delete.jsp" ; }
@RequestParam  correction parameter name    @RequestMapping ("/rightarggument")    String  logg3 (@ Requestparam ("name") String uname2) {        System.  out. println (uname2);         "/web-inf/delete.jsp";    } @RequestMapping ("/listargument")String listlogin5 (UserInfo info) {    System.  out. println (Info.getname () +"\ t"+info.getbooks (). Get (0). Getbookname ());         "/web-inf/delete.jsp";}}
Xml
Garbled Solution forceencoding
<!--javaweb Big three servlet Filter listener -<Filter> <Filter-name>charactorencodingfilter</Filter-name> <Filter-class>org.springframework.web.filter.characterencodingfilter</Filter-class> <Init-param> <Param-name>encoding</Param-name> <Param-value>utf-8</Param-value> </Init-param> <Init-param> <Param-name>forceencoding</Param-name> <Param-value>true</Param-value> </Init-param> </Filter> <filter-mapping> <Filter-name>charactorencodingfilter</Filter-name> <Url-pattern>/*</Url-pattern> </filter-mapping>

Top

SPRINGMVC Annotation Development

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.