page
Create a new folder page under WEB-INF, and then create a new page first.jsp.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
Spring core configuration file
Here you can configure the public prefix and suffix of a page, and you only need to give the view name in the control class.
<?xml version="1.0" encoding="UTF8"?>
<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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
In addition, I personally sorted out some information, and friends in need can click to receive it directly.
Java learning routes and materials from 0 to 1
Web.xml configuration file
This section is fixed, you don't need to register each Servlet separately, you only need to register a front controller DispatcherServlet.
<?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>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMVC-Servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Controller
SpringMVC provides many ways to mark a class as a control class; it can be done by implementing the Controller interface or by using Controller annotations.
Controller interface
Write a control class and then implement the Controller interface, rewrite the handleRequest method to realize the acceptance processing of the request, and finally return a ModelAndView class.
package com.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class InterfaceController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mv = new ModelAndView();
/*
1. 调用业务层
2. 封装数据、以及给出需要响应的视图名
3. 返回ModelAndView对象交给适配器
*/
mv.addObject("message","实现Controller接口"); //数据
mv.setViewName("first"); //响应的视图名
return mv;
}
}
Register the control class and give an access path. Finally visit localhost:8080/project name/inter/first; you can visit.
<bean id="/inter/first" class="com.controller.InterfaceController"/>
Controller annotation
I first learned about this annotation when I was in Spring. At that time, I only knew roughly that it could be used to register beans, and I didn't know that it could mark a class as a control class. The Controller annotation can register a bean because it is decorated by the Component annotation, and the Component annotation has the ability to register a bean.
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class AnnotationController {
@RequestMapping("/Annota/first") //注册映射路径
private String firstServlet(Model model){
model.addAttribute("message", "Controller注解"); //封装数据
return "first"; //给出视图名
}
}
The Controller annotation can be used to mark the class as a control class, and each method in it can be considered a Servlet. Compared to implementing the Controller interface, the annotation version is more powerful. When there are multiple Servlets, you only need to write multiple methods!
Only a little bit of annotation driver and MVC annotation support need to be extended in the SpringMVC core configuration file.
<!-- 扫描包注解 -->
<context:component-scan base-package="com.controller"/>
<!-- mvc注解驱动 -->
<mvc:annotation-driven/>
<!-- 默认处理器、过滤器 -->
<mvc:default-servlet-handler/>
RequestMapping annotation
The RequestMapping annotation is equivalent to registering a Servlet-a mapping of the path, indicating which method will be passed to access the /xxx path and nothing more; using this annotation can get rid of the core configuration file, and put a fixed template code in each configuration file. Controller annotation + RequestMapping annotation is used in the control class.
RequestMapping annotation modification class
When modifying the class, it means that each method under it needs to add a prefix path;
@Controller
@RequestMapping("/father")
public class RequestMappingController {
@RequestMapping("/test1")
private String test1(){
return "test1";
}
@RequestMapping("/test2")
private String test2(){
return "test2";
}
}
test1 real path: localhost:8080/project name/father/test1
test2 real path: localhost:8080/project name/father/test2
Modification method
It is only the specific path when modifying the method.
@Controller
public class RequestMappingController {
@RequestMapping("/test1")
private String test1(){
return "test1";
}
@RequestMapping("/father/test2")
private String test2(){
return "test2";
}
}
At last
Thank you for seeing this. If you think the article is helpful to you, remember to like it. Thank you for your support!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。