spring mvc
intercaptor对controller前后的操作
springmvc结构图
Spring MVC
JSON
JSON
文件上传
forward & redirect
Controller
import 的 model 是 “import org.springframework.ui.Model;”,而不是maven里的model类
Spring MVC
DispatcherServlet
Maven
修改中心仓库
mvn -version
Maven
Spring MVC
Spring MVC
MVC 架构
Front Controller
虽然和教程里的运行结果一样,但是很多地方我不理解,所以下面的代码里很可能有错误的地方
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>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--DispatcherServlet对应的上下文配置,默认为/WEB-INF/$servlet-name$-servlet.xml,下面语句改变了该默认参数 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/configs/spring/mvc-dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
------------------------------------------------------------------------------------------
mvc-dispatcher-servlet
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <!-- 让DispatcherServelt上下文只搜索@Controller标注的类,不搜索其他标注的类 --> <context:component-scan base-package="com.imooc.mvcdemo.controller" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <mvc:annotation-driven /> <!-- viewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="ViewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsps/" /> <property name="suffix" value=".jsp"/> </bean> </beans>
---------------------------------------------------------------------------------------------
pom.xml(与教程中不太一样,教程里的一些东西我没有写上去)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.how2java</groupId> <artifactId>springmvc2021071501</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springmvc2021071501 Maven Webapp</name> <url>http://maven.apache.org</url> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-framework-bom</artifactId> <version>${spring.version}</version> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.43</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.0-b07</version> </dependency> <dependency> <groupId>apache-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.1</version> <type>pom.lastUpdated</type> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.13.RELEASE</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.0.M1</version> <type>maven-plugin</type> </dependency> </dependencies> <build> <finalName>springmvc2021071501</finalName> <plugins> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.0.M1</version> </plugin> </plugins> </build> </project>
------------------------------------------------------------------------------------------
文件位置:
Controller处理
异步请求,赋值
引入Json类后,定义相关方法
json服务的2种模式
@RequestMapping(value="/{courseId}",method=RequestMethod.GET)
public @ResponseBody Course getCourseInJson(@PathVariable Integer courseId)
@RequestMapping(value="/jsontype/{courseId}",method=RequestMethod.POST)
public ResponseEntity<Course> getCourseInJson(@PathVariable Integer courseId)
文件上传:
继承Multipartfile类
绑定参数方法、重定向路径
Contrller 传统方式
利用HttpServletRequest处理请求参数,使用request.setAttribute("course",course)传参
// /courses/view3?courseId=456 形式的URL
@RequestMapping("view3")
public String viewCourse3(HttpServletRequest request){
Integer courseId = Integer.valueOf(request.getParameter("courseId"));
log.debug("In viewCourse3 ,courseId = {}",courseId);
Course course = courseService.getCoursebyId(courseId);
request.setAttribute("course",course);
return "course_overview";
}
@Controller 定义
@RequestMapping映射路径,定义被拦截路径
URL template(@RequestParam and @PathVariable) 入参定义方法,普通模式:@RequestParam ,Restful模式:@PathVariable
Controller 现代方式
Controller 现代方式
两种URL请求模
传统URL请求路径
Restful方式URL请求路径
两种Model定义
Model model定义,对应传参方法:model.addAttribute(course)
Map<String,Object> model定义,对应传参方法:model.put("course",course)
SpringMVC基本概念
SpringMVC概念:
什么是MVC?