Spring MVC creation and execution process

Keywords: Java Spring Spring MVC mvc

springMVC

Servlet------------>jsp

out.print() out.write()

model1 jsp + javabean

model2 MVC jsp+servlet+javabean

M:model Java class

5: View view

C: controller servlet

frame

ssh: struts1 | struts2 | springmvc spring hibernate

ssm: springmvc spring mybatis

jsp

jstl+el | ajax:json

M: Model ordinary java class mybatis: persistence layer, which directly interacts with data, POJO and service

dao does not need to implement classes. Create a mapping file with mybatis to implement interface methods

5: V iew jsp | html

No need to change

C: Controller servlet springmvc []

servlet becomes mvc

Transactions, monitoring and other businesses do not need to be managed by themselves, but are managed by spring

Client: send request to server: 1 > address 2 > form 3 > button

IDEA maven adding web to a normal project

The war package in pom.xml indicates that it is a web project

​ war

Ctrl+Alt+shift+S opens the page [the external chain picture transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-gnhb68oi-1632145255457) (C: \ users \ administrator \ appdata \ roaming \ typora \ typera user images \ image-20210918151738057. Png)]

Select moduel, select the module, and add the web with the plus sign. On the right is the location of the configuration file web.xml, and below is the location of the web folder. You can customize the settings. After completion, OK and refresh.

MVC creation steps

1. Configure web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springMVC.xml</param-value>
        </init-param>
<!--        take DispatcherServlet The creation time of is advanced tomcat At startup-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

Note: load on startup 1 load on startup

No, the servlet instance will be created only when the request is sent,

When set to 1, a servlet instance is created when the service is started

2. Create a control package and create specific classes and methods

Add note: @ Controller can be scanned by spring

Method adds @ RequestMapping("/hello") to specify the access address

The string returned by the method will be spliced with the prefix and suffix of the parser created in step 3 below, and the server will find and access it according to the spliced address

@Controller
public class controller {
    @RequestMapping("/hello")
    public String  hello(){
        System.out.println("----------");
        return "hello";
    }
}

3. Spring MVC configuration file

Create the springMVC.xml configuration file under the resource folder

Specify scan files, create view parsers, and specify prefixes and suffixes.

<!--Scan file-->   
<context:component-scan base-package="com.ljh.controller"></context:component-scan>
<!--view resolver -->      
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="WEB-INF/view/"></property>
  <property name="suffix" value=".jsp"></property>
</bean>

Note: the jsp file created under webapp is not safe and can be accessed directly through the address

Created under webapp/WEB-INF, it can only be accessed through request forwarding, but not through address

Notes:

requestMappig request mapping

Added to a class, it can be mapped at two levels

Path jump:

1,... / return to the previous level and jump by level

2. Start from the project root path ${pagecontext. Request. Contextpath}

Method specifies the request method: get/post/put/delete /

@RequestMapping(value = "/emplist",method = RequestMethod.GET)

Method mismatch 405

params specify parameters and get parameters

@RequestMapping("/dept")
public class deptController {
    @RequestMapping(value = "/deptlist" ,params = {"username","pwd"})
    public String list( String username ,String pwd){
        System.out.println(username+"-------"+pwd);
        return "deptlist";
    }
}

Note: during access, the parameters must match. If not, an error of 400 will be reported

restful style requests

Jump address http://localhost:8080/springMVC01/dept/deptlist/user=alice/pwd=123456

@Controller
@RequestMapping("/dept")
public class deptController {
    @RequestMapping(value = "/deptlist/{user}/{pwd}")
    public String list(@PathVariable("user") String username , @PathVariable("pwd") String pwd){
        System.out.println(username+"-------"+pwd);
        return "deptlist";
    }
}

blic String list(@PathVariable("user") String username , @PathVariable("pwd") String pwd){
System.out.println(username+"-------"+pwd);
return "deptlist";
}
}



Posted by chris9902 on Tue, 21 Sep 2021 04:57:24 -0700