springmvc框架

hello springmvc

什么是Spring MVC?
Spring MVC 為展現(xiàn)層提供的基于 MVC 設(shè)計理念的優(yōu)秀的Web 框架,是目前最主流的 MVC 框架之一。
Spring3.0 后全面超越 Struts2,成為最優(yōu)秀的 MVC 框架。

導(dǎo)入jar包

我們基于Spring mvc框架進(jìn)行開發(fā),需要依賴一下的spring jar包:

spring-aop-4.0.4.RELEASE.jar

spring-beans-4.0.4.RELEASE.jar

spring-context-4.0.4.RELEASE.jar

spring-core-4.0.4.RELEASE.jar

spring-expression-4.0.4.RELEASE.jar

spring-web-4.0.4.RELEASE.jar

spring-webmvc-4.0.4.RELEASE.jar

commons-logging-1.1.1.jar(用來打印log)

在WEB-INF目錄下新建lib文件夾,并將上面的jar包放入其中。

配置文件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>

注意: <param-value>classpath:springmvc.xml</param-value>用于配置spring mvc的配置文件的位置和名稱,這里說明會新建一個springmvc.xml的配置文件。

這里的servlet-mapping表示攔截的模式,這里是“*.do”,表示對于.do結(jié)尾的請求進(jìn)行攔截。

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監(jiān)聽的范圍,這里是在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 這樣的方式得到實際的物理視圖,然后會轉(zhuǎn)發(fā)操作
      * "/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”,即當(dāng)tomcat服務(wù)啟動后,在瀏覽器輸入這個url時,如果在這個方法打斷點了,就會跳入該方法。

3. 這個return的結(jié)果不是亂寫的,這個返回的字符串就是與上面springmvc.xml中進(jìn)行配合的,springmvc.xml中聲明了prefix和suffix,而夾在這兩者之間的就是這里返回的字符串,所以執(zhí)行完這個方法后,我們可以得到這樣的請求資源路徑“/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>

當(dāng)訪問index.jsp時,頁面上會展示一個超鏈接,點擊超鏈后,url中的地址就會發(fā)生跳轉(zhuǎn),由“http://localhost:8080/springTest/index.jsp”跳轉(zhuǎn)到“http://localhost:8080/springTest/helloworld.do”,而這個url請求就會進(jìn)入HelloWorld中的hello方法,因為其與該方法上的“/helloworld.do”匹配。

success.jsp(WEB-INF/views下)

該頁面是作為請求成功后的相應(yīng)頁面

<%@ 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時最常見的一種返回結(jié)果。需要在方法結(jié)束的時候定義一個ModelAndView對象,并對Model和View分別進(jìn)行設(shè)置。

@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進(jìn)行重定向那么地址欄中的URL會發(fā)生變化,同時不會攜帶上一次的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轉(zhuǎn)發(fā)

通過forward進(jìn)行轉(zhuǎn)發(fā),地址欄中的URL不會發(fā)生改變,同時會將上一次的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

返回這種結(jié)果的時候可以在Controller方法的形參中定義HTTPServletRequest和HTTPServletResponse對象進(jìn)行請求的接收和響應(yīng)

1)使用request轉(zhuǎn)發(fā)頁面
request.getRequestDispatcher("轉(zhuǎn)發(fā)路徑").forward(request,response);

2)使用response進(jìn)行頁面重定向
response.sendRedirect("重定向路徑");

3)也可以使用response指定響應(yīng)結(jié)果
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("轉(zhuǎn)發(fā)路徑").forward(request,response);
//        response.sendRedirect("重定向路徑");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().println("json串");
    }

以上三種返回值沒有什么重要和不重要的分別,一般來說都會使用到, 只不過有的時候使用的方式會有一些細(xì)微的差別

SpringMVC的各種參數(shù)綁定方式

  1. 基本數(shù)據(jù)類型(以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的參數(shù)變量名保持一致,就能完成數(shù)據(jù)綁定,如果不一致可以使用@RequestParam注解。需要注意的是,如果Controller方法參數(shù)中定義的是基本數(shù)據(jù)類型,但是從頁面提交過來的數(shù)據(jù)為null的話,會出現(xiàn)數(shù)據(jù)轉(zhuǎn)換的異常。也就是必須保證表單傳遞過來的數(shù)據(jù)不能為null。所以,在開發(fā)過程中,對可能為空的數(shù)據(jù),最好將參數(shù)數(shù)據(jù)類型定義成包裝類型,具體參見下面的例子。

  1. 包裝類型(以Integer為例,其他類似):
    Controller代碼:
@RequestMapping("saysth.do")
public void test(Integer count) {

}

表單代碼:

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

和基本數(shù)據(jù)類型基本一樣,不同之處在于,表單傳遞過來的數(shù)據(jù)可以為null,以上面代碼為例,如果表單中count為null或者表單中無count這個input,那么,Controller方法參數(shù)中的count值則為null。

  1. 自定義對象類型:
    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值一一匹配即可。

解決亂碼問題

web.xml里加入過濾器

<filter>

        <filter-name>characterEncodingFilter</filter-name>

        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

        <init-param>

            <param-name>encoding</param-name>

            <param-value>UTF-8</param-value>

        </init-param>

        <init-param>

            <param-name>forceEncoding</param-name>

            <param-value>true</param-value>

        </init-param>

    </filter>

    <filter-mapping>

        <filter-name>characterEncodingFilter</filter-name>

        <url-pattern>/*</url-pattern>

    </filter-mapping>

RESTful架構(gòu)

RESTful架構(gòu),就是目前最流行的一種互聯(lián)網(wǎng)軟件架構(gòu)。它結(jié)構(gòu)清晰、符合標(biāo)準(zhǔn)、易于理解、擴展方便,所以正得到越來越多網(wǎng)站的采用。RESTful(即Representational State Transfer的縮寫)其實是一個開發(fā)理念,是對http的很好的詮釋。

對url進(jìn)行規(guī)范,寫RESTful格式的url

非REST的url:http://...../queryItems.action?id=001

REST的url風(fēng)格:http://..../items/001

特點:url簡潔,將參數(shù)通過url傳到服務(wù)端

修改web.xml,添加DispatcherServlet的Restful配置

<servlet>

        <servlet-name>springmvc-servlet-rest</servlet-name>

               <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

               <init-param>

                   <param-name>contextConfigLocation</param-name>

                   <param-value>classpath:spring/springmvc.xml</param-value>

               </init-param>

           </servlet>

           <servlet-mapping>

                <servlet-name>springmvc-servlet-rest</servlet-name>

                <url-pattern>/</url-pattern>

            </servlet-mapping>

<url-pattern>/</url-pattern>表明所有url模式為/

URL 模板模式映射

@RequestMapping(value="/ viewItems/{id}")
{×××}占位符,請求的URL可以是“/viewItems/1”或“/viewItems/2”,通過在方法中使用@PathVariable獲取{×××}中的×××變量。@PathVariable用于將請求URL中的模板變量映射到功能處理方法的參數(shù)上。

 @RequestMapping("/viewItems/{id}") 
  public @ResponseBody viewItems(@PathVariable("id") String id) throws Exception{
     

 }

如果RequestMapping中表示為"/viewItems/{id}",id和形參名稱一致,@PathVariable不用指定名稱。

 @RequestMapping("/viewItems/{id}") 
  public @ResponseBody viewItems(@PathVariable String id) throws Exception{
     

 }

多個參數(shù)

@Controller  
@RequestMapping("/person") 
public class PersonAction{  
    
    @RequestMapping(value="/delete/{id}/{name}")
    public String delete(@PathVariable Integer id,@PathVariable String name){
        System.out.println(id + "   " + name) ;
        return "person";
    }
}  

靜態(tài)資源訪問<mvc:resources>

如果在DispatcherServlet中設(shè)置url-pattern為 /則必須對靜態(tài)資源進(jìn)行訪問處理,否則對css,js等文件的請求會被DispatcherServlet攔截。

spring mvc 的<mvc:resources mapping="" location="">實現(xiàn)對靜態(tài)資源進(jìn)行映射訪問。告訴springmvc框架,描述的靜態(tài)資源,無須DispatcherServlet攔截,以及查詢的目錄。

如下是對css和js文件訪問配置:

  <mvc:annotation-driven></mvc:annotation-driven>
 <mvc:resources location="/js/" mapping="/js/**"/>
 <mvc:resources location="/css/" mapping="/css/**"/>

上傳圖片

在頁面form中提交enctype="multipart/form-data"的數(shù)據(jù)時,需要springmvc對multipart類型的數(shù)據(jù)進(jìn)行解析。

在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>

上傳圖片代碼
頁面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="upload.do" method="post" enctype="multipart/form-data">
    <h2>文件上傳</h2>
    文件:<input type="file" name="file1"/><br/><br/>
    用戶名:<input type="text" name="username">
    <br/><br/>
    圖片:<img src="${imgpath}"/><br/><br/>
    <input type="submit" value="上傳"/>
</form>
</body>
</html>

controller方法

@Controller
public class uploadController {

   @RequestMapping("/upload.do")
   public void doUpload(@RequestParam MultipartFile file1, HttpServletRequest request) throws IOException {

       String strName = request.getParameter("username");
       System.out.println(strName);
   if(file1.isEmpty())
   {
       System.out.println("文件未上傳!");
   }
   else
   {
       //得到上傳的文件名
       String fileName = file1.getOriginalFilename();
       //得到服務(wù)器項目發(fā)布運行所在地址
       String strFolder = request.getServletContext().getRealPath("/image")+ File.separator;
       File folder = new File(strFolder);
       if(!folder.exists())
       {
           folder.mkdir();
       }
       //  此處未使用UUID來生成唯一標(biāo)識,用日期做為標(biāo)識
       String strNewFilePath = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+ fileName;
       String strFinalPath = strFolder + strNewFilePath;
       //查看文件上傳路徑,方便查找
       System.out.println(strFinalPath);
       //把文件上傳至path的路徑
       File localFile = new File(strFinalPath);
       file1.transferTo(localFile);
       request.getSession().setAttribute("imgpath", "image"+ File.separator+strNewFilePath);
       }

   }

}

攔截器

攔截器是用來動態(tài)攔截 action 調(diào)用的對象。它提供了一種機制可以使開發(fā)者可以定義在一個 action 執(zhí)行的前后執(zhí)行的代碼,也可以在一個 action 執(zhí)行前阻止其執(zhí)行,同時也提供了一種可以提取 action 中可重用部分的方式

image.png

HandlerInterceptor概述

在SpringMVC 中定義一個Interceptor是比較非常簡單,實現(xiàn)HandlerInterceptor接口。
HandlerInterceptor接口主要定義了三個方法:

  1. boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handle)方法:該方法將在請求處理之前進(jìn)行調(diào)用,只有該方法返回true,才會繼續(xù)執(zhí)行后續(xù)的Interceptor和Controller,當(dāng)返回值為true 時就會繼續(xù)調(diào)用下一個Interceptor的preHandle 方法,如果已經(jīng)是最后一個Interceptor的時候就會是調(diào)用當(dāng)前請求的Controller方法;
    2.void postHandle (HttpServletRequest request, HttpServletResponse response, Object handle, ModelAndView modelAndView)方法:該方法將在請求處理之后,DispatcherServlet進(jìn)行視圖返回渲染之前進(jìn)行調(diào)用,可以在這個方法中對Controller 處理之后的ModelAndView 對象進(jìn)行操作。
    3.void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handle, Exception ex)方法:該方法也是需要當(dāng)前對應(yīng)的Interceptor的preHandle方法的返回值為true時才會執(zhí)行,該方法將在整個請求結(jié)束之后,也就是在DispatcherServlet 渲染了對應(yīng)的視圖之后執(zhí)行。用于進(jìn)行資源清理。

簡單的一個例子:

xml需要配置:兩種配置方式(對所有的請求記性攔截,對特定的請求進(jìn)行攔截)

<mvc:interceptors>
        <!--對所有的請求記性攔截-->
        <!--<beans:bean class="com.sunp.common.interceptor.Myinterceptor"/>-->
        <!--對特定的請求進(jìn)行攔截-->
        <mvc:interceptor>
            <mapping path="/kfc/brands/brand1/*"/>
            <bean class="com.sunp.common.interceptor.Myinterceptor"/>
        </mvc:interceptor>
</mvc:interceptors>

interceptors類

public class Myinterceptor implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("preHandle run!");
        return true;
    }

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

        System.out.println("postHandle run!");
    }
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

        System.out.println("afterCompletion run!");
    }
}

需求

1、用戶請求url

2、攔截器進(jìn)行攔截校驗

如果請求的url是公開地址(無需登陸即可訪問的url),讓放行

如果用戶session 不存在跳轉(zhuǎn)到登陸頁面

如果用戶session存在放行,繼續(xù)操作。

<mvc:interceptors>  

<!--如果配置了多個攔截器,則按順序執(zhí)行 -->  

<!-- 登陸認(rèn)證攔截器 -->  

<mvc:interceptor>  

               <!-- /**表示所有url包括子url路徑 -->  

    <mvc:mapping path="/**"/>  

    <bean class="cn.itcast.ssm.interceptor.LoginInterceptor"></bean>  

</mvc:interceptor>  

</mvc:interceptors>  

定義攔截器,實現(xiàn)HandlerInterceptor接口,實現(xiàn)該接口提供了3個方法:


public class LoginInterceptor implements HandlerInterceptor{  

      

    //進(jìn)入Handler方法之前執(zhí)行  

    //用于身份認(rèn)證,身份授權(quán)  

    //比如身份認(rèn)證,如果認(rèn)證通過表示當(dāng)前用戶沒有登陸,需要此方法攔截不再往下執(zhí)行  

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,  

            Object handler) throws Exception {  

        //獲取請求的url  

        String url = request.getRequestURI();  

        //判斷url是否是公開地址(實際使用時將公開地址配置配置文件中)  

        //這里公開地址是登陸提交的地址  

        if(url.indexOf("login.action")>0){  

            return true;  

        }  

          

        //判斷session  

        HttpSession session = request.getSession();  

        //從session中取出用戶身份信息  

        String username = (String) session.getAttribute("username");  

          

        if(username != null){  

            //身份存在,放行  

            return true;  

        }  

          

        //執(zhí)行到這里時表示用戶身份需要認(rèn)證,跳轉(zhuǎn)到登陸頁面  

        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);  

        return false;  

    }  
 

    //進(jìn)入Handler方法之后,返回modelAndView之前執(zhí)行  

    //應(yīng)用場景從modelAndView出發(fā):將公用的模型數(shù)據(jù)(比如菜單導(dǎo)航)在這里傳到視圖,也可以在這里統(tǒng)一指定視圖  

    public void postHandle(HttpServletRequest request, HttpServletResponse response,  

            Object handler, ModelAndView modelAndView) throws Exception {  

        // TODO Auto-generated method stub  

        System.out.println(" LoginInterceptor..postHandle");  

    }  

  

    //執(zhí)行Handler完成后執(zhí)行此方法  

    //應(yīng)用場景:統(tǒng)一異常處理,統(tǒng)一日志處理  

    public void afterCompletion(HttpServletRequest request,  

            HttpServletResponse response, Object handler, Exception ex)  

            throws Exception {  

        // TODO Auto-generated method stub  

        System.out.println("LoginInterceptor..afterCompletion");  

    }  

  

}  

登錄的controller方法

@Controller  
public class LoginController {  

  

    // 登陸  

    @RequestMapping("/login")  

    public String login(HttpSession session, String username, String password)  

            throws Exception {  

  

        // 調(diào)用service進(jìn)行用戶身份驗證  

     
        // 在session中保存用戶身份信息  

        session.setAttribute("username", username);  

        // 重定向到商品列表頁面  

        return "redirect:/items/queryItems.action";  

    }  

  

    // 退出  

    @RequestMapping("/logout")  

    public String logout(HttpSession session) throws Exception {  

        // 清除session  

        session.invalidate();  


        // 重定向到商品列表頁面  

        return "redirect:/items/queryItems.action";  

    }  

  

}  

攔截器無法攔截jsp

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,273評論 6 342
  • Jar包下載 commons-logging-1.2-bin.zip springframework 創(chuàng)建工程 i...
    Mr丶sorrow閱讀 386評論 0 0
  • 此文為轉(zhuǎn)載,謝謝! 人生最糟糕的不是貧困,不是厄運,而是不知道想要什么,不知道如何選擇,只有那些有明確目標(biāo)、懂得選...
    金軻閱讀 447評論 0 0
  • 做一棵樹不枉 春去秋來 聽過鳥語,聞過花香 多情的風(fēng)一生停留 如此,再不會錯過 你的音訊
    馬流云閱讀 759評論 0 2

友情鏈接更多精彩內(nèi)容