springmvc框架

什么是Spring MVC?
Spring MVC 为展现层提供的基于 MVC 设计理念的优秀的Web 框架,是目前最主流的 MVC 框架之一。
Spring3.0 后全面超越 Struts2,成为最优秀的 MVC 框架。
配置文件web.xml(WEB-INF下)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
id="WebApp_ID" version="3.1">
  
    <!-- 配置DispatchcerServlet -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
         <!-- 配置Spring mvc下的配置文件的位置和名称 -->
         <init-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>classpath:springmvc.xml</param-value>
         </init-param>
         <load-on-startup>1</load-on-startup>
     </servlet>
     <servlet-mapping>
         <servlet-name>springDispatcherServlet</servlet-name>
         <url-pattern>*.do</url-pattern>
     </servlet-mapping>
</web-app>

注意: classpath:springmvc.xml用于配置spring mvc的配置文件的位置和名称,这里说明会新建一个springmvc.xml的配置文件。

表示拦截的所有“*.do”结尾的请求。

Springmvc.xml(scr下)
  在src目录下新建springmvc.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: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-4.0.xsd
         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
         <!-- 配置自动扫描的包 -->
         <context:component-scan base-package="com.neusoft.controller"></context:component-scan>
         <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
             <property name = "prefix" value="/WEB-INF/views/"></property>
             <property name = "suffix" value = ".jsp"></property>
         </bean>
 </beans>

**<context:component-scan base-package=“com.neusoft.controller”></context:component-scan>**表示spring监听的范围,这里是在com.neusoft.controller下。
  
  HelloWorldController.java(com.neusoft.controller下)

package com.neusoft.springmvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWorldController {
     /**
      * 1. 使用RequestMapping注解来映射请求的URL
      * 2. 返回值会通过视图解析器解析为实际的物理视图, 对于InternalResourceViewResolver视图解析器,会做如下解析
      * 通过prefix+returnVal+suffix 这样的方式得到实际的物理视图,然后会转发操作
      * "/WEB-INF/views/success.jsp"
      * @return
      */
     @RequestMapping("/helloworld.do")
     public String hello(){
         System.out.println("hello world");
         return "success";
     }
}

1.首先要在类的前面添加“Controller”注解,表示是spring的控制器,这里会写一个方法hello()
2. hello方法上方有一个@RequestMapping, 是用于匹配请求的路径,比如这里匹配的请求路径就是“http://localhost:8080/helloworld.do”,即当tomcat服务启动后,在浏览器输入这个url时,如果在这个方法打断点了,就会跳入该方法。

  1. 这个return的结果不是乱写的,这个返回的字符串就是与上面springmvc.xml中进行配合的,springmvc.xml中声明了prefix和suffix,而夹在这两者之间的就是这里返回的字符串,所以执行完这个方法后,我们可以得到这样的请求资源路径“/WEB-INF/views/success.jsp”,这个success.jsp是需要我们新建的

index.jsp(WebContent下)
在新建success.jsp之前,我们需要有一个入口,也就是这里的index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="helloworld.do">hello world</a>
</body>
</html>

当访问index.jsp时,页面上会展示一个超链接,点击超链后,url中的地址就会发生跳转,由“http://localhost:8080/springTest/index.jsp”跳转到“http://localhost:8080/springTest/helloworld.do”,而这个url请求就会进入HelloWorld中的hello方法,因为其与该方法上的“/helloworld.do”匹配。

success.jsp(WEB-INF/views下)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h4>Success Page</h4>
</body>
</html>

Controller方法的返回值

1、返回ModelAndView

返回ModelAndView时最常见的一种返回结果。需要在方法结束的时候定义一个ModelAndView对象,并对Model和View分别进行设置。

@Controller
public class HelloWorldController {
    @RequestMapping("/helloworld.do")
    public String hello(){
        System.out.println("hello world");
        return "success";
    }

    @RequestMapping("/abc.do")
    public ModelAndView abc(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("username","zhangsan");
        modelAndView.setViewName("success");
        return modelAndView;

    }

}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
      ${username}
</body>
</html>

2、返回String

1):字符串代表逻辑视图名

真实的访问路径=“前缀”+逻辑视图名+“后缀”

注意:如果返回的String代表逻辑视图名的话,那么Model的返回方式如下:

@RequestMapping("/helloworld.do")
    public String hello(Model model){
        model.addAttribute("username","zhangsan");
        System.out.println("hello world");
        return "success";
    }

2):代表redirect重定向

redirect的特点和servlet一样,使用redirect进行重定向那么地址栏中的URL会发生变化,同时不会携带上一次的request

@Controller
public class HelloWorldController {
    @RequestMapping("/helloworld.do")
    public String hello(Model model){
        model.addAttribute("username","zhangsan");
        System.out.println("hello world");
        return "success";
    }

    @RequestMapping("/abc.do")
    public ModelAndView abc(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("username","zhangsan");
        modelAndView.setViewName("success");
        return modelAndView;

    }

    @RequestMapping("/redirect.do")
    public String redirect(){
          return "redirect:login.do";
    }

}

3):代表forward转发

通过forward进行转发,地址栏中的URL不会发生改变,同时会将上一次的request携带到写一次请求中去

@Controller
public class HelloWorldController {
    @RequestMapping("/helloworld.do")
    public String hello(Model model){
        model.addAttribute("username","zhangsan");
        System.out.println("hello world");
        return "success";
    }

    @RequestMapping("/abc.do")
    public ModelAndView abc(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("username","zhangsan");
        modelAndView.setViewName("success");
        return modelAndView;

    }

    @RequestMapping("/redirect.do")
    public String redirect(){
          return "forward:login.do";
    }

}

3、返回void

返回这种结果的时候可以在Controller方法的形参中定义HTTPServletRequest和HTTPServletResponse对象进行请求的接收和响应

1)使用request转发页面
request.getRequestDispatcher(“转发路径”).forward(request,response);

2)使用response进行页面重定向
response.sendRedirect(“重定向路径”);

3)也可以使用response指定响应结果
response.setCharacterEncoding(“UTF-8”);
response.setContentType(“application/json;charset=utf-8”);
response.getWriter().println(“json串”);

在这里插入@RequestMapping("/returnvoid.do")
    public void returnvoid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.getRequestDispatcher("转发路径").forward(request,response);
//        response.sendRedirect("重定向路径");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().println("json串");
    }

SpringMVC的各种参数绑定方式
1.基本数据类型(以int为例,其他类似):
Controller代码:

@RequestMapping("saysth.do")
public void test(int count) {

}

表单代码:

<form action="saysth.do" method="post">
<input name="count" value="10" type="text"/>
</form>

表单中input的name值和Controller的参数变量名保持一致,就能完成数据绑定,如果不一致可以使用@RequestParam注解。需要注意的是,如果Controller方法参数中定义的是基本数据类型,但是从页面提交过来的数据为null的话,会出现数据转换的异常。也就是必须保证表单传递过来的数据不能为null。所以,在开发过程中,对可能为空的数据,最好将参数数据类型定义成包装类型,具体参见下面的例子。

2.包装类型(以Integer为例,其他类似):
Controller代码:

@RequestMapping("saysth.do")
public void test(Integer count) {

}

表单代码

<form action="saysth.do" method="post">
      <input name="count" value="10" type="text"/>
</form>

和基本数据类型基本一样,不同之处在于,表单传递过来的数据可以为null,以上面代码为例,如果表单中count为null或者表单中无count这个input,那么,Controller方法参数中的count值则为null。

3.自定义对象类型:
Model代码:

public class User {
    private String firstName;
    private String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

Controller代码:

@RequestMapping("saysth.do")
public void test(User user) {

}

表单代码:

<form action="saysth.do" method="post">
<input name="firstName" value="张" type="text"/>
<input name="lastName" value="三" type="text"/>
......
</form>

将对象的属性名和input的name值一一匹配即可。

上传图片

在页面form中提交enctype="multipart/form-data"的数据时,需要springmvc对multipart类型的数据进行解析。

在springmvc.xml中配置multipart类型解析器。

<!-- 文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="80000"></property>
    <property name="defaultEncoding" value="UTF-8"></property>
</bean>

页面

 <form method="post" action="${pageContext.request.contextPath}/upload" enctype="multipart/form-data">

用户名:<input type="text" name="username">
    <input type="file" name="pic">
    <input type="submit">
  </form>

controller方法

@Controller
public class UploadController {
    @RequestMapping("/upload")
    public ModelAndView upload(String username, @RequestParam MultipartFile pic, HttpServletRequest request) throws IOException {
        ModelAndView modelAndView = new ModelAndView();
        //得到用户在input输入的文字
        System.out.println(username);
        if (pic.getSize()>0) {
            //得到图片的名字,方便查看图片是否已保存成功;
            String filename = pic.getOriginalFilename();
            //项目存储的系统路径
            String realPath = request.getServletContext().getRealPath("/img")+File.separator;
            File folder = new File(realPath);
            if (!folder.exists()) {
                folder.mkdir();
            }
            System.out.println(realPath);
            System.out.println(filename);
//            Date date=new Date();
//            long time=date.getTime();
            UUID uuid=UUID.randomUUID();

            //把传过来的图片指定到保存到D盘
            File file1 = new File(realPath+uuid+ filename);
            //把图片存到磁盘中
            pic.transferTo(file1);
            modelAndView.addObject("picname", filename);
            //返回到index界面
            modelAndView.setViewName("index");
        }
        return modelAndView;
    }

拦截器

拦截器是用来动态拦截 action(方法) 调用的对象。它提供了一种机制可以使开发者可以定义在一个 action 执行的前后执行的代码,也可以在一个 action 执行前阻止其执行,同时也提供了一种可以提取 action 中可重用部分的方式。

HandlerInterceptor概述
在SpringMVC 中定义一个Interceptor是比较非常简单,实现HandlerInterceptor接口。
HandlerInterceptor接口主要定义了三个方法:

boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handle)方法:该方法将在请求处理之前进行调用,只有该方法返回true,才会继续执行后续的Interceptor和Controller,当返回值为true 时就会继续调用下一个Interceptor的preHandle 方法,如果已经是最后一个Interceptor的时候就会是调用当前请求的Controller方法;
2.void postHandle (HttpServletRequest request, HttpServletResponse response, Object handle, ModelAndView modelAndView)方法:该方法将在请求处理之后,DispatcherServlet进行视图返回渲染之前进行调用,可以在这个方法中对Controller 处理之后的ModelAndView 对象进行操作。
3.void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handle, Exception ex)方法:该方法也是需要当前对应的Interceptor的preHandle方法的返回值为true时才会执行,该方法将在整个请求结束之后,也就是在DispatcherServlet 渲染了对应的视图之后执行。用于进行资源清理。
简单的一个例子:
1.login.jsp

<form action="${pageContext.request.contextPath}/login" method="post">
    姓名:<input type="text" name="username" >
    密码:<input type="text" name="pwd">
    <input type="submit" value="提交">
</form>

LoginController类

@Controller
public class LoginController {

    @RequestMapping("/tologin")
    public String tologin(){
        return "login";
    }
    @RequestMapping("/login")
    public String select(String username, String pwd, HttpServletRequest request){
        if (username.equals("admin")&&pwd.equals("123456")){
            HttpSession httpSession=request.getSession();
            httpSession.setAttribute("userinfor",username);
            return"index";
        }
           return "login";
    }

index.jsp

  <h1>欢迎您</h1>${userinfor}
  <a href="/tologin">登录</a>

web.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_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:spring.xml</param-value>
 </init-param>

        <!-- 配置Spring mvc下的配置文件的位置和名称 -->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

spring.xml

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**"></mvc:mapping>
        <bean class="com.neusoft.interceptor.FirstInterceptor"></bean>
    </mvc:interceptor>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <!--//指定不拦截-->
            <mvc:exclude-mapping path="/login"></mvc:exclude-mapping>
            <mvc:exclude-mapping path="/tologin"></mvc:exclude-mapping>
            <bean class="com.neusoft.interceptor.LoginInterceptor"></bean>
        </mvc:interceptor>
</mvc:interceptors>

LoginInterceptor类

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        HttpSession httpSession=httpServletRequest.getSession();
        Object userin = httpSession.getAttribute("userinfor");
            if(userin!=null){
        return true;
    }else {
                httpServletRequest.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(httpServletRequest,httpServletResponse);
            }
     return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值