Spring MVC will know that you won’t ask to hack me


Spring MVC will know that you won't ask to hack me


Sort out some necessary knowledge. If you can’t, comeKill the bloggerbar

Spring MVC will know that you won't ask to hack me

Common notes

Bind the request parameter to the method parameter of your controller
Case study:

Syntax: @ requestparam (value = "parameter name", required = "true / false", DefaultValue = ")
Value: parameter name
Required: whether the parameter is included. The default value is true, which means that the parameter must be included in the request path. If not, an error will be reported.
Default value: the default parameter value. If the value is set, required = true will be invalid and will automatically be false. If the parameter is not passed, the default value will be used
    public ModelAndView test16(@RequestParam("name")String name){
        ModelAndView mv = new ModelAndView();
        mv.addObject ("MSG", "receive common request parameter): + name)";
        return mv;

Fill in the value from a URI template

@RequestMapping(value = "/getbyid", method = RequestMethod.GET)
    private Map<String, Object> getbyid( HttpServletRequest request,@RequestParam(value="id", required=true)int idnum){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        modelMap.put("idnum", idnum);
        return modelMap;

@The requestparam and @ pathvariable annotations are used to receive requests from requests. Both can receive parameters. The key difference is that @ requestparam receives requests from requestsTake the value in the request, and @ pathvariable is derived fromFill in a URI template

@RequestMappingIs an annotation used to handle request address mapping. It can be used on classes and methods. Annotation on a class maps a specific request or request pattern to a controller, indicating that all methods responding to requests in the class take the address as the parent path; Annotation on the method level indicates the mapping relationship further specified to the processing method.

@Requestmapping ("/ student"); // also equivalent to @ requestmapping (value = / student ");

There are three common attributes:
value: Specifies the actual address of the request. Value can be omitted;

method Attribute: Specifies the type of request, mainly including get, put, post and delete. The default is get.

produces Property: specify the type of returned content, such as productions = “application / JSON; charset = UTF-8”.

Auto scan module

Manually configure bean

In the XML configuration file, declare a bean or component, and the spring container will check and register your bean or component

<bean id="customerService" class="com.lei.customer.services.CustomerService">
        <property name="customerDAO" ref="customerDAO" />

<bean id="customerDAO" class="com.lei.customer.dao.CustomerDAO" />
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});
CustomerService cust = (CustomerService)context.getBean("customerService");

Using auto scan components

Using @ component @ controller @ service @ repository

 <context:component-scan base-package="com.lei.customer" />

Custom scan component name

public class CustomerService 
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});
CustomerService cust = (CustomerService)context.getBean("AAA");

Filter components in scanning

1. Spring is commonly used in many configurations- common.xml And spring- MVC.xml Separate configuration, this configuration is the same as their duties, it is particularly clear.

In spring- MVC.xml inJust scan @ controllerAs a controller, I don’t do anything else.

In spring- common.xml inOnly scan annotations for some transaction logic

2. Given a project package organization:



Let’s first give these two package mechanisms

staySpring-MVC.xmlThere are the following configurations in:

<! -- scan @ controller annotation -- >
<context:component-scan base-package="com.fq.controller">
    <context:include-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />

You can see that you need to write the final package instead of base package = ” com.fq ”。 For < cide > include filter, it will scan, not just @ controller. Ha ha ha, this needs attention. It usually leads to a common error, that is, the transaction does not work. The remedy is to add use default filters = “false”.

In spring- common.xml There are the following configurations in the

<! -- configure scan annotation, do not scan @ controller annotation -- >
<context:component-scan base-package="com.fq">
    <context:exclude-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />

As you can see, he’s going to scan com.fq All subclasses under the package do not contain @ controller. aboutexculude-filterThere is no problem of scanning after inaccurate packets.

Spring MVC will know that you won't ask to hack me

It’s not easy to create. If this article can help you, please give me your support. Give me a rose, a fragrance in my hand, and I’ll be happy with you

Recommended Today

Practice of query operation of database table (Experiment 3)

Following the previous two experiments, this experiment is to master the use of select statements for various query operations: single table query, multi table connection and query, nested query, set query, to consolidate the database query operation.Now follow Xiaobian to practice together!Based on the data table (student, course, SC, teacher, TC) created and inserted in […]