Spring MVC annotations Access to the controller and how the form data is received, including methods for receiving date types and objects directly

Source: Internet
Author: User
Tags add time

Configuring Scanners in Spring

<!--SPRINGMVC Scanner--

<context:component-scan base-package= "Com.beifeng.servlet"/>

Building a controller class

@Controller

@RequestMapping (value= "/sys/")

Public class Regcontroller {

@RequestMapping (value= "Reg.do", Method=requestmethod. POST)

Public String Reg () {

System. out. println ("-User Registration--");

return "Index";

}

}

Note When using annotations cannot be used The mapping of the class name controller that controls the class, otherwise it will not be found

<bean class= "Org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" ></bean>

The data that receives the foreground form can also be received through the httpservletrequest or through the form field name or through the object.

    1. Array of receive direct write array
    2. A date type's receive can be formatted by using the Initbinder function to define the date conversion

/**

* Get page data via HttpServletRequest

*

* @return

*/

@RequestMapping ("Reg1.do")

Public String REG1 (HttpServletRequest request) {

String userId = Request.getparameter ("UserId");

String USERPW = Request.getparameter ("USERPW");

SYSTEM.OUT.PRINTLN ("Account number:" + userId);

System.out.println ("Password:" + USERPW);

Return "index";

}

/**

* Get your Name

*

* @return

*/

@RequestMapping ("Reg2.do")

public string REG2 (string userName) {

System.out.println ("Name:" + userName);

Return "index";

}

@RequestMapping ("Reg3.do")

Public String reg3 (Integer userId, String userName, Date userbrithday) {

SYSTEM.OUT.PRINTLN ("Account number:" + userId);

System.out.println ("Name:" + userName);

System.out.println ("Birthday:" + userbrithday);

Return "index";

}

/**

* Gets the value of the check box: Multiple values

*

* @param Xqs

* @return

*/

@RequestMapping ("Reg4.do")

Public String REG4 (string[] xqs) {

for (String Xq:xqs) {

System.out.println ("Interest:" + XQ);

}

Return "index";

}

/**

* Get page parameters by object

*

* @param Xqs

* @return

*/

@RequestMapping ("Reg5.do")

Public String reg5 (UserInfo user) {

SYSTEM.OUT.PRINTLN (user);

Return "index";

}

/**

* Add time to the property editor

*/

@InitBinder

public void Initbinder (Servletrequestdatabinder bin) {

Bin.registercustomeditor (Date.class, New Customdateeditor (

New SimpleDateFormat ("Yyyy-mm-dd"), true);

}

Spring MVC annotations Access to the controller and how the form data is received, including methods for receiving date types and objects directly

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.