SpringMVC basic study notes (1) ---- Simple use of SpringMVC

SpringMVC4
              jar: the original Spring jar plus
Spring-context-support: Support UI template, mail service, cache cache
Spring-webmvc: Implementation of mvc
 
The basic steps:
 
1. Create a class to implement the Controller interface
2. Create a ModelAndView object and add information to this object
3.ModelAndView.setViewName();
4. Create a folder under WEB-INF, and put the jsp file in the folder
5.setViewName fill in /WEN-INF/....
6. Register Controller: springmvc.xml under src (any)
7.xml add bean constraints
<? xml version = "1.0" encoding = "UTF-8" ?>
< beans xmlns = "http://www.springframework.org/schema/beans"
    xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context = "http://www.springframework.org/schema/context" xsi:schemaLocation = "
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd" >
 
8. Register Controller:
        <!-- Restrict the requests processed by mvc to .do, which must start with a slash-->
        < bean id = "/my.do" class = "handlers.MyController" > </ bean >
 
9. Register the central scheduler: registered in web.xml, it is a servlet
  < servlet >
       < servlet-name > mvc </ servlet-name >
         < servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class >
  </ servlet >
  < servlet-mapping >
       < servlet-name > mvc </ servlet-name >
       < url-pattern > *.do </ url-pattern >
  </ servlet-mapping >
cannot configure /*
 
Use ${message} in the jumped JSP page to display the data added by the Model.addObject() method
//${requestScope.message} can also output
 
Running out FileNotFoundException:
Could not open ServletContext resource [/WEB-INF/mvc-servlet.xml]
 
Because the default is to find the servlet-name-servlet.xml configuration file under WEB-INF;
 
So rename the configuration file and put it under WEB-INF
 
program can run
 
 
Problem: The file name and file location are fixed (can only be placed in servlet-name-servlet.xml under WEB-INF), not good
Solution: The SpringMVC configuration file loaded by DispatcherServlet, modify the properties of DispatcherServlret (in the parent class)
 
Modify the contextConfigLocation attribute, not in the <context-param> (ServletContext) but in the servlet's <init-param>.
       < init-param >
             < param-name > contextConfigLocation </ param-name >
             < param-value > classpath:springmvc.xml </ param-value >
       </ init-param >
 
Reboot, success.
 
Problem: The servlet is created by the container when it is first accessed (a servlet needs to be initialized when accessing), which makes the response speed slower
Solution: The load-on-startup tag of <servlet>:
< load-on-startup > 1 </ load-on-startup > --- the smaller the load-on-startup of multiple servlets, the higher the priority, and the integers greater than 0 are meaningful
 
Problem mv.setViewName(); The address of the resource inside is too long
There is another way of writing mv.setViewName: View resolver: equivalent to piecing together the url
1. Create a bean (without id) internal view resolver with two properties: prefix and suffix:
 
        < bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" >
             < property name = "prefix" value = "/WEB-INF/anyfolder/" />
             < property name = "suffix" value = ".jsp" />
        </ bean >
 
Run the program directly, success
 
it's here
mv .setViewName( "welcome" );
welcome is the logical view name, after splicing it is the physical view name

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324875834&siteId=291194637