SpringMVC_ based on annotation development

1. SpringMVC annotation development
1.1 Create project
1.2 Improve project
1.3 Import dependency
1.4 Configure web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>dispatcherServle</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>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServle</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

1.5 Configure SpringMVC configuration file

<?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:mvc="http://www.springframework.org/schema/mvc"
       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/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <!--开启注解-->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!--配置自动扫描包-->
    <context:component-scan base-package="com.wangxing.springmvc.controller"></context:component-scan>
    <!--配置视图解析器-->
    <!--org.springframework.web.servlet.view.InternalResourceViewResolver-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix"  value=""></property>
    </bean>
</beans>

1.6 Create a controller

package com.wangxing.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
    @RequestMapping("/test1.do")
    public ModelAndView  testRequest(){
        ModelAndView  mav=new ModelAndView();
        mav.addObject("info","hello,网星软件");
        mav.setViewName("test.jsp");
        return  mav;
    }
}

1.7 Create jsp
1.8 Configure service deployment project
1.9 Test http://localhost:8080/spingmvc2/test1.do

2. @Controller annotation
@Controller—Indicates that the java class we have written is a controller class that processes requests.
Can only be used in java classes.
In the javaweb program, it is layered out. For the table name java class is a controller, we use @Controller
@Controller contains @Component.
@Controller and @Service and @Repository that we learned in spring mark the application as different layers.
Data access layer------@Repository
business access layer------@Service
Web layer【Control layer】----@Controller
webapp------------Static resource
3 .@RequestMapping
sets the access path of the controller class/request processing method
@RequestMapping can be used on the java class to indicate the configuration of the access path of this java class;

package com.wangxing.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/hello")
public class HelloController{
    @RequestMapping("/test1.do")
    public ModelAndView  testRequest(){
        ModelAndView  mav=new ModelAndView();
        mav.addObject("info","hello,网星软件");
        mav.setViewName("test.jsp");
        return  mav;
    }
}

http://localhost:8080/spingmvc2/hello/test1.do
If there is no @RequestMapping("/hello") in the controller class, then we can directly use the request processing method @RequestMapping("/ access path of test1.do").

package com.wangxing.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController{
    @RequestMapping("/test1.do")
    public ModelAndView  testRequest(){
        ModelAndView  mav=new ModelAndView();
        mav.addObject("info","hello,网星软件");
        mav.setViewName("test.jsp");
        return  mav;
    }
}

http://localhost:8080/spingmvc2/test1.do
@RequestMapping can also act on the request processing method, which means configuring the access path of the request processing method.
Commonly used attributes of @RequestMapping
1.value means to set the access path [can be omitted]

@RequestMapping(value = "/test1.do")

Can be omitted

@RequestMapping("/test1.do")

Can wildcards be set when setting the access path
? : Match any single character
For example: @RequestMapping("/?hello.test")
http://localhost:8080/spingmvc2/atest1.do
http://localhost:8080/spingmvc2/test1.do //error
http:/ /localhost:8080/spingmvc2/hhhtest1.do //Error
*: matches any number of characters
For example: @RequestMapping("/ hello.test")
http://localhost:8080/spingmvc2/test1.do
http://localhost: 8080/spingmvc2/wtest1.do
http://localhost:8080/spingmvc2/wwwtest1.do
For example: @RequestMapping("/
/hello.test")
http://localhost:8080/spingmvc2/w/test1.do
http: //localhost:8080/spingmvc2/www/test1.do
http://localhost:8080/spingmvc2/test1.do //error
http://localhost:8080/spingmvc2/hhhh/www/test1.do //error
**: Match multiple paths
For example: @RequestMapping("/**/hello.test")
http://localhost:8080/spingmvc2/test1.do
http://localhost:8080/spingmvc2/w/test1.do
http://localhost:8080 /spingmvc2/www/test1.do
http://localhost:8080/spingmvc2/hhh/www/test1.do 2.
method—restricts the access method of the request [GET, POST...]
Expression form: @RequestMapping(value = “/ login.do”,method = RequestMethod.POST)
Index.jsp

<%@page language="java" pageEncoding="UTF-8" %>
<html>
<body>
  <form action="test1.do" method="get">
    <input type="submit" value="测试Method属性"/>
  </form>
</body>
</html>
package com.wangxing.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController{
    @RequestMapping(value = "/test1.do",method = RequestMethod.POST)
    public ModelAndView  testRequest(){
        ModelAndView  mav=new ModelAndView();
        mav.addObject("info","hello,网星软件");
        mav.setViewName("test.jsp");
        return  mav;
    }
}

Modify the form submission radiation in the index.jsp page to post to succeed.
4. The request processing method receives the request parameter value
1. @PathVariable defines the method to obtain the parameter data on the request url path.
For example: the
request processing method:

@RequestMapping(value = "/get1/{username}/{password}",method = RequestMethod.GET)
public ModelAndView  getReqParam1(@PathVariable("username")String name,
@PathVariable("password")String pass){
    ModelAndView  mav=new ModelAndView();
    mav.addObject("username",name);
    mav.addObject("password",pass);
    mav.setViewName("test.jsp");
    return  mav;
}

http request: http://localhost:8080/spingmvc2/get1/zhangsan/123456
Note: The central control of the web.xml file/
2.@RequestParam is defined in the method, and the parameters passed in the request through key=value are obtained Data
e.g.
request processing method

@RequestMapping(value = "/get2",method = RequestMethod.GET)
public ModelAndView  getReqParam2(@RequestParam("username")String name, @RequestParam("password")String pass){
    ModelAndView  mav=new ModelAndView();
    mav.addObject("username",name);
    mav.addObject("password",pass);
    mav.setViewName("test.jsp");
    return  mav;
}

http请求:http://localhost:8080/spingmvc2/get2?

username=lisi&password=000000
The *.do
request processing method of the central controller of the web.xml file : @RequestMapping(value = "/get2.do",method = RequestMethod.GET)
http request: http://localhost:8080/spingmvc2/get2.do?username=lisi&password=000000
3. getParameter of the HttpServletRequest object () Method to receive data
For example:
request processing method

@RequestMapping(value = "/get3.do",method = RequestMethod.GET)
public ModelAndView  getReqParam3(HttpServletRequest  request){
    String name=request.getParameter("username");
    String pass=request.getParameter("password");
    ModelAndView  mav=new ModelAndView();
    mav.addObject("username",name);
    mav.addObject("password",pass);
    mav.setViewName("test.jsp");
    return  mav;
}

http请求:http://localhost:8080/spingmvc2/get3.do?

username=wangwu&password=111111

4. Define the corresponding parameter variable in the request processing method. The name of the parameter variable is the same
as the value of the name attribute of the page element. For example:
login.jsp

<%@page language="java" pageEncoding="UTF-8" isELIgnored="false" %>
<html>
<body>
<form action="get4.do" method="post">
    用户名:<input type="text" name="username"/><br>
    密码:<input type="password" name="password"/><br>
    <input type="submit" value="提交"/>
</form>
</body>
</html>

Request processing method

@RequestMapping(value = "/get4.do",method = RequestMethod.POST)
public ModelAndView  getReqParam4(String username,String password){
    ModelAndView  mav=new ModelAndView();
    mav.addObject("username",username);
    mav.addObject("password",password);
    mav.setViewName("test.jsp");
    return  mav;
}

5. Encapsulate the request parameter value that needs to be submitted into the java object [The name of the member variable of the java object must be the same as the name attribute value of the page element]
For example:
register.jsp

<%@page language="java" pageEncoding="UTF-8" isELIgnored="false" %>
<html>
<body>
<form action="get5.do" method="post">
    用户名:<input type="text" name="username"/><br>
    密码:<input type="password" name="password"/><br>
    年龄:<input type="text" name="age"/><br>
    地址:<input type="text" name="address"/><br>
    日期:<input type="text" name="day"/><br>
    <input type="submit" value="提交"/>
</form>
</body>
</html>

PersonBean.java

package com.wangxing.springmvc.bean;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
public class PersonBean {
    private  String username;
    private  String password;
    private  Integer age;
    private  String address;
    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date day;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public Date getDay() {
        return day;
    }
    public void setDay(Date day) {
        this.day = day;
    }
}

Request processing method:

@RequestMapping(value = "/get5.do",method = RequestMethod.POST)
public ModelAndView  getReqParam5(PersonBean personBean){
    ModelAndView  mav=new ModelAndView();
    mav.addObject("personBean",personBean);
    mav.setViewName("test.jsp");
    return  mav;
}

Note: The name of the member variable of the java object must be the same as the name attribute value of the page element.
6. Organize the submitted request parameters into json data [described later]
SpringMVC access static resources
When we use SPringMVC to access HTML files, a 404 appears Error, because SpringMVC does not support access to static resources [.html / .js / .css /.jpg...]
Option 1: Activate Tomcat's defaultServlet to process static files [web.xml]

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>

Write in front of DispatcherServlet, let defaultServlet intercept first, this will not enter Spring, I think the performance is the best.
Option 2: mvc:resources is provided in spring 3.0.4 and later versions

<!--对静态资源文件的访问-->
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/css/**" location="/css/" />

Guess you like

Origin blog.csdn.net/guoguo0717/article/details/110225495