Spring Framework--day02 Common configuration and annotations

Source: Internet
Author: User

One, SPRINGMVC common configuration
1.SPRINGMVC the configuration file that is loaded by default is Springmvc-servlet.xml under Web-inf, and we typically place the configuration file in the project's resource directory, which is classpath, so you need to configure the SPRINGMVC-loaded profile address.
Transfer the configuration file to resources, rename it, and modify Web. xml:

2. The processor mapper, processor adapter, and view resolver applied in the above configuration are default and can be modified to use for annotations.
Add the configuration in the Springmvc configuration file:

  <!--Annotated Processor adapter--<bean class= " Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter "/> <!--view parser through logical view: in Dex View Resolver Parse logical view (parse out physical view): prefix (prefix) + logical view +suffix (suffix) Resolution physical view:/index.jsp---<bean class= "ORG.SPRINGFR Amework.web.servlet.view.InternalResourceViewResolver "> <property name=" prefix "value="/"></property&        Gt <property name= "suffix" value= ". JSP" ></property> </bean> Note: Annotations The configuration of processor mapper and processor adapters can be combined into one configuration: < Mvc:annotation-driven/>  

3. In the annotation mode configuration, the controller's writing will become very simple:
@Controller
public class TestController {
@RequestMapping ("/test1")//Configure access path for this method
Public String test1 () {
System.out.println ("hello,springmvc!");
Return "index";
}
@RequestMapping ("/test2")
Public String test2 () {
System.out.println ("hello,test2!");
Return "index";
} }
Second, the parameters of the method in the controller
The parameter list of the methods in the controller is very powerful and the main functions are as follows two points:
1. Receive request parameters; 2. Get Servletapi

1. Receive Request parameters
To write a user logon method:
For simple types of parameters, SPRINGMVC can directly encapsulate the types declared in the argument list, such as string,int,double ...
public string Login (string username, string password) {
SYSTEM.OUT.PRINTLN (username);
SYSTEM.OUT.PRINTLN (password);
return null;
}

 //or you can receive a Java Bean public String login (user user) {System.out.println (User.getusername ()) directly;        System.out.println (User.getpassword ());    return null;        If the request parameter is a date, SPRINGMVC cannot be encapsulated directly into date, and the date format needs to be set: public class User {private String username;        private String password;        @DateTimeFormat (pattern = "YYYY-MM-DD") private Date birthday; ...    } If the request parameter is an array type, we can receive it directly from the array: @RequestMapping ("Delsel") public String Delsel (integer[] IDs) {Phoneservice.dels        El (IDs);    return "Redirect:list"; If you want to use the collection type to receive array parameters?    Is it possible to make the following wording?        @RequestMapping ("Delsel") public String Delsel (arraylist<integer> IDs) {Phoneservice.delsel (IDS);    return "Redirect:list"; }

2. Get Servletapi
The following types can be used directly in the parameters of the Controller method:
HttpServletRequest
Obtaining request information through the Requests object
HttpServletResponse
Handling response information through response
HttpSession
To get the objects stored in the session by the Session object
Model/modelmap
Model is an interface, MODELMAP is an interface implementation.
Role: Populates the model data into the domain.

        public String login(User user, HttpServletRequest request, HttpSession session,                            HttpServletResponse response, Model model)

Third, the return value of the method in the controller
You can specify three return value types for methods in the controller:
Modelandview
Specify the view and place the model data in the domain
void
If the return value is void, you can define the request and response on the controller method parameter, using request or response to specify the response result
String
Logical View Name
Four, springmvc commonly used annotations
@RequestMapping
Declaration on the method:
@RequestMapping (value = "/list", method = Requestmethod.get)
To configure the access path for the method by using the Value property
Use the Method property to specify how access is allowed

声明在类上:    @RequestMapping("/person")        窄化请求,可以对请求URL进行分类管理,限制访问该类中的URL必须先访问class类上加上的@RequestMapping注解。        例如:/person/add,/person/list...

@RequestParam
This annotation is used to annotate a request parameter:
In form:

Controller:
public string Login (@RequestParam (value= "username", required=true,defaultvalue= "Noname") string uname)
Value: @RequestParam (value= "username") is equivalent to @requestparam ("username")
Required: parameter is required
DefaultValue: Setting default values

@PathVariable
Take a part of the path as the basis of the request parameter, restful.
@RequestMapping ("/findbyid/{id}")
Public String FindByID (@PathVariable Integer ID, model) {
Phone phone = Phoneservice.findbyid (ID);
Model.addattribute ("Phone", phone);
System.out.println ("..................)
Return "edit";
}

Spring Framework--day02 Common configuration and annotations

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.