SpringMVC基础
概念
SpringMVC简介
配置
添加依赖 Spring-webmvc(4.3.6)八个jar包添加进来
配置web.xml文件: 让所的/请求都交给了我DispatcherServlet,而DispatcherServlet里面需要配置一个contextConfigLocation,上下文配置路径,也就是我们的SpringMVC的配置文件。如果没有显式地配置该属性,SpringMVC会在默认WEB-INF下去找[servlet-name]-servlet.xml文件
<?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_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Spring-mvc.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/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">
<!--
配置SpringMVC的视图解析器
可以分别指定前缀和后缀
prefix: /WEB-INF/view/,那么控制器那边会在虚拟视图前拼接该字符串
suffix:.jsp .html,那么控制器那边会在虚拟视图后面拼接该字符串
拼接完字符串的效果
/WEB-INF/view/index.html
/WEB-INF/view/detail.jsp
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<!--<property name="suffix" value=".jsp" />-->
</bean>
<!--配置缺省的servlet处理器,静态资源可以直接被访问-->
<mvc:default-servlet-handler />
<!--
bean的id属性值不能包含特殊字符
name可以,所以路径需要使用name来标识一个控制器的路径
指定name对应路径交给哪个控制器来进行具体的处理
-->
<bean name="/ProductInput" class="com.qfedu.controller.ProductInputController" />
<bean name="/SaveProductController" class="com.qfedu.controller.SaveProductController" />
</beans>
ProductInputController.java
package com.qfedu.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
-
SpringMVC配置文件中让/ProductInput请求会交给当前类来进行处理,自动执行Controller的回调方法
*/
public class ProductInputController implements Controller {/**
- /ProductInput请求会回调该方法
- @param request
- @param response
- @return 视图和模型对象,只需要展示页面,则需要一个单纯的虚拟视图名即可
- @throws Exception
*/
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
//return new ModelAndView("/WEB-INF/view/index.jsp");
return new ModelAndView("index.html");
}
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>this is product input html page.</h1>
<form method="post" action="SaveProductController">
pid:<input type="text" name="pid" /><br />
pname:<input type="text" name="pname" /><br />
price:<input type="text" name="price" /><br />
<input type="submit" value="submit" /><br />
</form>
</body>
</html>
在当前的表单页面上输入信息提交,交给一个新的请求/SaveProductController,那么发出一个新的请求,先交给DispatcherServlet,然后读取SpringMVC配置文件,发现该请求交给了SaveProductController控制器
package com.qfedu.controller;
import com.qfedu.bean.Product;
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 SaveProductController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
String spid = request.getParameter("pid");
String pname = request.getParameter("pname");
String sprice = request.getParameter("price");
int pid = spid == null ? 0 : Integer.parseInt(spid);
double price = sprice==null ? 0.0 : Double.parseDouble(sprice);
Product p = new Product();
p.setPid(pid);
p.setPname(pname);
p.setPrice(price);
/**
* 创建模型视图对象
* 该代码等同于以前的两行代码
* request.setAttribute("p", p);
* request.getRequestDispatcher("detail.jsp").forward(request, response);
*/
ModelAndView mv = new ModelAndView("detail.jsp", "p", p);
return mv;
}
}
detail.jsp
<%--
Created by IntelliJ IDEA.
User: james
Date: 2020/3/3
Time: 11:27 AM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>detail</title>
</head>
<body>
<h1>this is product detail page.</h1>
<h3>pid : ${p.pid}</h3>
<h3>pname : ${p.pname}</h3>
<h3>price : ${p.price}</h3>
</body>
</html>