赞
踩
基础环境
springmvc 环境
jdk1.8
tomcat8.5
集成环境
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 MyController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("my");
return modelAndView;
}
}
在 webapp/WEB-INF/customer 目录新建 my.jsp 文件
<%-- Created by IntelliJ IDEA. User: - Date: 2023/2/20 Time: 22:02 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>my.jsp 页面</h1> </body> </html>
springmvc核心配置文件
springmvc-config.xml
<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 1.配置前端控制器放行静态资源(html/css/js等,否则静态资源将无法访问) --> <mvc:default-servlet-handler /> <!-- 2.配置注解驱动,用于识别注解(比如@Controller) --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 3.配置需要扫描的包:spring自动去扫描 base-package 下的类, 如果扫描到的类上有 @Controller、@Service、@Component等注解, 将会自动将类注册为bean @Controller public class HelloController{} --> <context:component-scan base-package="com.shaoming.controller"> </context:component-scan> <!-- 4.配置内部资源视图解析器 prefix:配置路径前缀 suffix:配置文件后缀 /WEB-INF/pages/home.jsp --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/customer/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 默认编码 --> <property name="defaultEncoding" value="utf-8" /> <!-- 文件大小最大值 --> <property name="maxUploadSize" value="10485760000" /> <!-- 内存中的最大值 --> <property name="maxInMemorySize" value="40960" /> <!-- 启用是为了推迟文件解析,以便捕获文件大小异常 --> <property name="resolveLazily" value="true" /> <property name="uploadTempDir" value="fileUpload/temp" /> </bean> <bean id="/hello.action" class="com.shaoming.controller.MyController"></bean> </beans>
localhost:8080/mvc/hello.action
springmvc 核心 前端控制器 DispatcherServlet
DispatcherServlet --> doDispatch
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Kj6GPGmi-1676902699688)(D:\my-note\typora-picture\image-20230220221332534.png)]
getHandle() 获取处理器
BeanNameUrlHandlerMapping
获取处理器适配器
略
ha.handler(xx) 处理请求
最后跳转自己定义的controller实现类
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。