springMVC study notes (two): controller parameters

One, mybatis paging plug-in (PageHelper plug-in)

Document address: View document

1. Database paging

Database paging: physical paging and memory paging
1) Memory paging : first load all data into the memory, and then find the paged data from the memory.
2) Physical paging : When data is retrieved, only paging data is retrieved and the data is returned to the client.
MyBatis provides memory paging: RowBounds parameter.
Mybatis physical paging:
1. Write your own sql statement sql limit ?,?
2. MyBatis plug-in MyBatis PageHelper

2. Steps to use PageHelper

Step 1: Lead people jar

pagehelper-5.1.9.jar
jsqlparser-2.1.jar

Step 2: Spring integration (spring-datasource.xml)

       <!--分页插件  -->
       <property name="plugins">
        <array>
            <bean class="com.github.pagehelper.PageInterceptor">
                <!-- 这里的几个配置主要演示如何使用,如果不理解,一定要去掉下面的配置 -->
                <property name="properties">
                    <value>
                        helperDialect=mysql
                        reasonable=true
                        supportMethodsArguments=true
                        params=count=countSql
                        autoRuntimeDialect=true
                    </value>
                </property>
            </bean>
        </array>
    </property>

Step 3: Call the paging method at the service layer

		PageHelper.startPage(2, 3);
		List<Users> list = usersMapper.selectByExample(null);
		System.out.println(list);

Note:
1) PageHelper.startPage(2, 3); Query the 2nd page and display 3 entries per page.
2) Only the first Mybatis query (Select) method following the PageHelper.startPage method will be paged.

Second, the display of Date data type in jsp

Method: format library of jstl of jsp (c library)

第1步:<%@taglib uri=“http://java.sun.com/jstl/fmt_rt” prefix=“fmt”%>

Step 2: <fmt:formatDate value="${user.birthday}" pattern="yyyy-MM-dd HH:mm:ss"/>
format the Date type data passed by the controller into a string in the specified format date.

				**注**:HH是24小时制      hh是12小时制

Three, spring mvc processor @controller parameter binding

1. Basic type parameter binding: automatic binding

Requirements : The parameters of the processor method must be consistent with the parameters of the url.

2. Pojo object type parameter binding: automatic binding

Requirements : The name of the expressed element must be consistent with the attribute name of the pojo.

3. The default parameters of the control method: request, response, model, session.
4. Collection

1) Array

Note: Do not delete in batches one by one, you should
delete from users where id in (33,57) through batch delete statement

2) list collection

3) Map collection

5. Controller parameters and other things

1)、@RequestParam

@RequestParam(name=“pageNum”,defaultValue=“1”)
Function 1) The default value can be set
Function 2) The parameter name passed from the front end of the parameter mapping is inconsistent with the parameter name of the back-end controller interface

2), restFul style-parameter transfer

http://localhost:8080/application name/user/delete?id=33 non-restFul style transfer parameters
http://localhost:8080/application name/user/delete/33 restFul style transfer parameters
Note: @PathVariable is required for parameters Bind

Supplement :

  1. The response code of the http request:
    404 : Cannot find the resource
    500 : System code error
    200 : The request is successful
    400 : The Bad Request request cannot be accepted-General parameter binding failed [Key]
  2. Spring mvc handles the issue of date parameter binding [Key point]:
    Method 1: @DateTimeFormat(pattern=“yyyy-MM-dd”) Realization
    premise : spring-web.xml enables mvc annotation configuration
<mvc:annotation-driven></mvc:annotation-driven>替换
                    
<!-- 1.映射器:HandlerMapping  url==>handler -->

<bean  class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>

<!-- 2.适配器: HandlerAdatper   调用 Handler==>Controller-->

<bean  class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>

mvc:annotation-driven supports @DateTimeFomat and @NumberFormat

Note: mvc:annotation-driven configuration automatically loads RequestMappingHandlerMapping and RequestMappingHandlerAdapter

Method 2: @InitBinder will only automatically convert the date of the current Controller's String to Date

/**
    * 注册属性编辑器(字符串转换为日期)
   */
   @InitBinder
   public void initBinder(WebDataBinder binder) throws Exception {
    
    
       binder.registerCustomEditor(Date.class, new CustomDateEditor(
               new SimpleDateFormat("yyyy-MM-dd"),true));
   }

Insert picture description here

	 特别注意:必须在web.xml中配置spring的编码过滤器,否则乱码。

Method three: spring mvc extension plug-in-HandlerMethodArgumentResolver (understand)

The form ----->controller method edit(Users users)
HandlerMethodArgumentResovle is used to process the client to pass the request parameters to the Controller method
for automatic parameter binding.
Click for details

Previous chapter: springMVC study notes (1): springmvc development process

Guess you like

Origin blog.csdn.net/weixin_46822085/article/details/108984250