Introduction to spring MVC for java learning
Introduction to spring MVC for java learning
0x00 Preface
The two frameworks of SSM, mybatis and spring, were written earlier. Here, let's write about the spring MVC framework.
0x01 spring MVC overview
0x02 spring MVC code implementation
Configure web XML file:
<?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>
<!-- 设置默认加载spring 配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<!-- 使用dispatcherServlet 并设置拦截路径-->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Read sprng MVC is set here XML file, we also need to create a spring MVC XML file, and then configure it
Configure spring MVC XML file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<!-- 注解扫描-->
<context:component-scan base-package="com.test">
</context:component-scan>
<!--配置视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--配置spring开启注解mvc支持-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
Use context: component scan to start annotation scanning, and then you can directly use the annotation method to load the class into the container.
Colltroller class:
package com.test.domain;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloContraoller {
@RequestMapping(path = "/hello")
public String sayHello(){
System.out.println("Hello spring mvc");
return "success";
}
}
@Controller is marked on a class, and the class marked with it is a spring MVC controller object. The distribution processor will scan the methods of the classes that use the annotation.
@The requestmapping annotation method indicates that the annotated method will be executed if the path specified by the annotation.
Properties of requestmapping:
1. path 指定请求路径的url
2. value value属性和path属性是一样的
3. mthod 指定该方法的请求方式
4. params 指定限制请求参数的条件
5. headers 发送的请求中必须包含的请求头
Then you can write JSP pages to test.
index. JSP page:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="/hello">入门</a>
</body>
</html>
Set request mode
We can also specify the request method in the @ requestmapping annotation.
@Controller
public class HelloContraoller {
@RequestMapping(path = "/hello",method = RequestMethod.POST)
public String sayHello(){
System.out.println("Hello spring mvc");
return "success";
}
}
Add the method attribute in the annotation and specify it as the post attribute. If you use get to request, you will find that the request will fail.
Set request parameters
Add params attribute to the requestmapping annotation to specify parameters.
@Controller
public class HelloContraoller {
@RequestMapping(path = "/hello",params = {"username"})
public String sayHello(){
System.out.println("Hello spring mvc");
return "success";
}
}
If there are no parameters, the method is not executed.
Parameter binding
Method. When submitting parameters, the MVC framework will help us get the value and pass it into the method.
@Controller
public class HelloContraoller {
@RequestMapping(path = "/hello",params = {"username","password"})
public String sayHello(String username,String password){
System.out.println("username"+username);
System.out.println("password"+password);
return "success";
}
}
Parameter binding entity class
Define an entity class that receives parameters
public class Person {
private String username;
private String password;
public String getUsername() {
return username;
}
@Override
public String toString() {
return "Person{" +
"username='" + username + '\'' +
",password='" + password + '\'' +
'}';
}
public void setUsername(String username) {
this.username = username;
}
public String getpassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Contraoller class:
}
@RequestMapping(path = "/tijiao","password"},method = RequestMethod.POST)
public String submit(Person person){
System.out.println(person);
return "success";
}
Here, you only need to set the incoming parameter to the defined entity class.
Submit page:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/tijiao" method="post" >
账户:<input type="text" name="username">
密码: <input type="password" name="password">
<input type="submit" name="提交">
</form>
</body>
</html>
The name of the form submitted in the JSP page should be the same as the member variable name of the entity class, otherwise it cannot be automatically encapsulated.
0x03 end
Spring MVC is actually much simpler than the previous ones, but it will be troublesome in XML configuration.