springMVC-- first demo

 

 

 

 

SpringMVC core is the central scheduler (front controller).

Front-end controller configuration in the web.xml file. For receiving a user-initiated request. After receiving the user's request, calls the mapper, the query processor to perform the requested chain; After calling adapter, according to the processor execution chain, call processor Controller, returns the MAV; After calling view resolver, generates a corresponding view of a process class that view objects, the final view rendering, and the results returned to the user.

 

Development steps:

1. Create a new Dynamic Web Project project

2. Import required jar package

 

3. The key, modify the web.xml. Register the DispatcherServlet central scheduler object (also called front controller).

4. Define a request to initiate a view jsp

The definition of the object processor processing the user's request, Controller interface defining class implements.

6. The definition of the view results display processor jsp, EL expressions may be used to obtain data memory.

7. springMVC defined profile, springMVC is part of the spring, but also the container. Registered processor custom objects.

8. Register springMVC view resolver object configuration file.

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>01my-springmvc</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <File-available for purchase> the index.jsp </ File-available for purchase> .html </ File-available for purchase> 
    <File-available for purchase> default .htm </ File-available for purchase> 
    <File-available for purchase> default .jsp </ File-available for purchase> 
  </ is available for purchase-File-List> 
  <-! springmvc is the core dispatcher scheduler, inherited the HttpServlet -> 
  <the servlet> 
      <the servlet-name> springmvc </ the servlet-name> 
      <servlet- class > org.springframework. web.servlet.DispatcherServlet </ servlet- class > 
      <-! 
          when the program starts, create DispatcherServlet object loaded springMVC need to profile 
          the default configuration file is / the WEB-INF / Spring - the servlet.xml 
          default file name is   <servlet-name> plus -
    <welcome-file>default
         the servlet.xml
          
          SpringMVC specified profile using the parent FrameworkServlet the contextConfigLocation
        -> 
      
      <the init-param> 
          <param-name> the contextConfigLocation </ param-name> 
          <param-value> CLASSPATH: springmvc.xml </ param-value> 
      </ the init -param> 
      
      ! <-   the DispatcherServlet created when the application starts tomcat examples 
      
          Load -On- startup: start time specified object instance creating a value in the server
               1 is a number greater than or equal to 0, the smaller the value, the more time to create the object. early.
              2 . Do not give value or to a negative value, when the first visit of the Servlet, it creates an instance of an object.
              3 . When the load-on- a plurality of program objects startup values are the same, the order is determined by the server object is created. 
              HTTPS: // blog.csdn.
       -> 
      <Load-ON-Startup> . 1 </ Load-ON-Startup> 
  </ the servlet> 
  <-Mapping the servlet> 
      <the servlet-name> SpringMVC </ the servlet-name> 
      <-!   
          URL - pattern: the a some browsers to request DispatcherServlet process, the request to be processed through DispatcherServlet springmvc framework for dealing with requests. 
          There are two ways springmvc 
              1 . extension, e.g. * do   , .mvc *, * .action the like, can not be used * .jsp  
               2 . A slash   " / " indicates that all requests
        -> 
      <URL-pattern> * . do </ URL-pattern> 
  </ Mapping the servlet-> 
</ Web-App>

 

 

<?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"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        HTTP: // www.springframework.org/schema/beans 
        HTTP: // www.springframework.org/schema/beans/spring-beans.xsd 
           HTTP: // www.springframework.org/schema/context 
           HTTP: // the WWW .springframework.org / Schema / context / the Spring-context.xsd 
           HTTP: // www.springframework.org/schema/aop 
           HTTP: // www.springframework.org/schema/aop/spring-aop.xsd 
           HTTP: // www.springframework.org/schema/tx 
           HTTP: // www.springframework.org/schema/tx/spring-tx.xsd "
    > 
     <- registered handler object -!>
    
    <-!  
        ID: Specifies the URI request, the name of the resource to   / at the beginning, indicates that the user's request on bean is used to separate and other bean
       -> 
    <bean ID = " /hello.do "  class = " controller.MyController " /> 
</ Beans>

 

package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class MyController implements Controller {

    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView mv =  new ModelAndView();
        String name = request.getParameter("name");
        
        //把参数处理之后,在页面显示
        //框架处理之后, 效果相当于  request.setAttribute("name", "hello"+name);
        mv.addObject("name", "hello"+name);
        
        //request.getRequestDispatcher("/show.jsp").forward(request, response);
        mv.setViewName("/show.jsp");
        return mv;
    }

}

 

Guess you like

Origin www.cnblogs.com/llq1214/p/11427030.html