SpringMVC重要组件: DispatcherServlet:前段控制器,接受所有请求,过滤jsp;【必须进行配置使得xml文件生效】 |
HandlerMapping:解析请求格式,判断希望调用那个方法;【必须进行配置】 |
HandlerAdapter:负责调用具体的方法;【走默认,不必在xml中进行配置】 进行页面跳转 req.getRequestDispatcher("index.jsp").forward(req, resp); |
ViewResovler:视图解析器,解析结果,准备跳转到具体的物理视图。 |
|
Web.xml无法打包的jar包。即使打包到jar包中也无效。Tomcat在加载项目的时候必须到web-info中查找web.xml,其他任何路径无效,jar包中的东西都在隐藏的classes文件中,所以必须自己配置。
视图解析器:
xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>springmvc01display-name> <welcome-file-list> <welcome-file>index.htmlwelcome-file> <welcome-file>index.htmwelcome-file> <welcome-file>index.jspwelcome-file> <welcome-file>default.htmlwelcome-file> <welcome-file>default.htmwelcome-file> <welcome-file>default.jspwelcome-file> welcome-file-list> <servlet> <servlet-name>springmvcservlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:springmvc.xmlparam-value> init-param>
<load-on-startup>1load-on-startup>
servlet> <servlet-mapping> <servlet-name>springmvcservlet-name> <url-pattern>/url-pattern> servlet-mapping> web-app>
|
package com.pshdhx.test;
import org.springframework.context.ApplicationContext; import org.springframework.web.servlet.HandlerAdapter; import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.ViewResolver;
public class Test { public static void main(String[] args) { ApplicationContext ac = null; //加载dispatcherServlet就创建了ApplicationContext的接口-ConfigurableApplicationContext的容器。加载xml文件 HandlerMapping hm = null; //urlMapping HandlerAdapter ha = null; //走方法中的语句 ViewResolver rs = null; //走解析路径
} }
|
package com.pshdhx.controller;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller;
public class DemoController implements Controller {
@Override public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { System.out.println("执行了控制器DemoController"); //走默认适配器 ModelAndView mv = new ModelAndView("main"); //走默认视图解析器 return mv; }
}
|
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="demo123" class="com.pshdhx.controller.DemoController">bean> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap"> <map>
<entry key="demo" value-ref="demo123">entry> map> property> bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/">property> <property name="suffix" value=".jsp">property> bean> beans> |
父类的父类往上找: |
WebApplication是spring容器 |
说明了spring和springmvc是父子容器 Springmvc能够调用spring容器中的方法。
|
因为service是在spring容器中,所以在controller中允许注入service方法 请求方法,放行请求 |
|
|
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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.pshdhx.controller">context:component-scan>
<mvc:annotation-driven>mvc:annotation-driven>
<mvc:resources location="/js/" mapping="/js/**">mvc:resources> beans> |
package com.pshdhx.controller;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;
@Controller //把这个类交给容器去管理 public class DemoController { @RequestMapping("demo") //使用注解 ==控制器逻辑名 public String demo(){ System.out.println("执行demo"); return "main.jsp"; } }
|
<filter> <filter-name>encodingfilter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class> <init-param> <param-name>encodingparam-name> <param-value>UTF8param-value> init-param> filter> <filter-mapping> <filter-name>encodingfilter-name> <url-pattern>/*url-pattern> filter-mapping> |
|
package com.pshdhx.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;
@Controller public class DemoController03 { @RequestMapping("demo03") public String controller(String username,int age,HttpServletRequest req){ System.out.println("执行demo03"); System.out.println(""+username+"***"+age+"通过action的value到控制器方法的参数,控制jsp页面传值到控制器"); req.setAttribute("demo03", "测试从java控制器传值到jsp页面"); return "main.jsp"; } } |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title heretitle> head> <body> main.jsp ${demo03 } body> html> |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title heretitle> head> <body> <form action="demo03" method="post"> <input type="text" name="username"><br> <input type="text" name="age"><br> <input type="submit" value="提交"><br> form> body> html> |
|
|
如果没有参数,报错500,不能正常跳转 但是如果int变为Integer,则能够正常跳转 Int默认值:
|
|
|
添加redirect路径: 添加forward路径:
|
表示前边的demo中不需要/了 Redirect和forward表示直接跳转到文件,而不是在某个路径下的文件。 |
不加forward:表示查找demo11的jsp页面 404 加上forward:可以找到demo11的控制器 success
|
|
|
加上responseBody后,不管返回值是什么,恒不跳转。在页面输出返回值。 |
responseBody以jackson形式底层实现,需要引入上面三个包。 |
设置响应头contentType的形式。 |
|
|
能打开就打开,打不开就下载
|
Classpath:被编译到了这里
|
|
package com.pshdhx.controller; import javax.servlet.ServletContext; /** * 测试内置对象作用域 */ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;
@Controller public class DemoController { @RequestMapping("demo1") public String demo1(HttpServletRequest req,HttpSession sessionParam){ //测试request的作用域 req.setAttribute("req", "req的值");
//测试session和sessionParam HttpSession session = req.getSession(); session.setAttribute("session", "session的值"); sessionParam.setAttribute("sessionParam", "sessionParam的值");
//application 但是直接定义servletcontext的参数不行:是一个接口,不允许实例化和注入。 ServletContext application = req.getServletContext(); application.setAttribute("application", "application的值");
return "/index.jsp"; } }
|
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title heretitle> head> <body> request:${requestScope.req }<br> session:${sessionScope.session }<br> sessionParam:${sessionScope.sessionParam }<br> application:${applicationScope.application }
body> html> |
xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>springmvc01display-name> <welcome-file-list> <welcome-file>index.htmlwelcome-file> <welcome-file>index.htmwelcome-file> <welcome-file>index.jspwelcome-file> <welcome-file>default.htmlwelcome-file> <welcome-file>default.htmwelcome-file> <welcome-file>default.jspwelcome-file> welcome-file-list>
<servlet> <servlet-name>springmvcservlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> <init-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:springmvc.xmlparam-value> init-param> <load-on-startup>1load-on-startup> servlet> <servlet-mapping> <servlet-name>springmvcservlet-name> <url-pattern>/url-pattern> servlet-mapping>
<filter> <filter-name>encodingfilter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class> <init-param> <param-name>encodingparam-name> <param-value>UTF8param-value> init-param> filter> <filter-mapping> <filter-name>encodingfilter-name> <url-pattern>/*url-pattern> filter-mapping> web-app> |
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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.pshdhx.controller">context:component-scan>
<mvc:annotation-driven>mvc:annotation-driven>
<mvc:resources location="/js/" mapping="/js/**">mvc:resources> beans> |
|
若果是rar的话,直接下载,但是txt是直接打开。 |
|
|
package com.pshdhx.controller;
import java.io.File; import java.io.IOException; import java.util.UUID;
import org.apache.commons.io.FileUtils; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile;
/** * 测试文件上传需要引入commoms-fileload和commons-io两个jar包 * 并且form标签中要 :enctype="multipart/form-data" type="file" * @author Administrator * */ @Controller public class DemoController { @RequestMapping("upload") public String upload(MultipartFile file,String name) throws IOException{ String fileName = file.getOriginalFilename(); String suffix = fileName.substring(fileName.lastIndexOf(".")); //判断上传文件类型 if(suffix.equalsIgnoreCase(".png")){ String uuid = UUID.randomUUID().toString(); FileUtils.copyInputStreamToFile(file.getInputStream(), new File("E:/"+uuid+suffix)); return "/index.jsp"; }else{ return "error.jsp"; } } }
|
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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.pshdhx.controller">context:component-scan>
<mvc:annotation-driven>mvc:annotation-driven>
<mvc:resources location="/js/" mapping="/js/**">mvc:resources> <mvc:resources location="/file/" mapping="/file/**">mvc:resources>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="50">property> bean>
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">/error.jspprop> props> property> bean> beans> |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title heretitle> head> <body> <form action="upload" enctype="multipart/form-data" method="post"> 姓名:<input type="text" name="name"><br> 文件:<input type="file" name="file"><br> <input type="submit" value="上传"> form> body> html> |
过程截图: |
|
|
|
|