springmvc框架

hello springmvc

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

導入jar包

我們基于Spring mvc框架進行開發(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é)尾的請求進行攔截听诸。

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”,即當tomcat服務啟動后敛惊,在瀏覽器輸入這個url時渊鞋,如果在這個方法打斷點了,就會跳入該方法瞧挤。

3. 這個return的結(jié)果不是亂寫的锡宋,這個返回的字符串就是與上面springmvc.xml中進行配合的,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>

當訪問index.jsp時,頁面上會展示一個超鏈接衡奥,點擊超鏈后爹袁,url中的地址就會發(fā)生跳轉(zhuǎn),由“http://localhost:8080/springTest/index.jsp”跳轉(zhuǎn)到“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時最常見的一種返回結(jié)果。需要在方法結(jié)束的時候定義一個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會發(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進行轉(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對象進行請求的接收和響應

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

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

3)也可以使用response指定響應結(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串");
    }

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

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)清晰秦忿、符合標準麦射、易于理解、擴展方便灯谣,所以正得到越來越多網(wǎng)站的采用潜秋。RESTful(即Representational State Transfer的縮寫)其實是一個開發(fā)理念,是對http的很好的詮釋胎许。

對url進行規(guī)范峻呛,寫RESTful格式的url

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

REST的url風格:http://..../items/001

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

修改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中設置url-pattern為 /則必須對靜態(tài)資源進行訪問處理,否則對css北秽,js等文件的請求會被DispatcherServlet攔截葡幸。

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

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

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

上傳圖片

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

在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();
       //得到服務器項目發(fā)布運行所在地址
       String strFolder = request.getServletContext().getRealPath("/image")+ File.separator;
       File folder = new File(strFolder);
       if(!folder.exists())
       {
           folder.mkdir();
       }
       //  此處未使用UUID來生成唯一標識,用日期做為標識
       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

HandlerInterceptor概述

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

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

簡單的一個例子:

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

<mvc:interceptors>
        <!--對所有的請求記性攔截-->
        <!--<beans:bean class="com.sunp.common.interceptor.Myinterceptor"/>-->
        <!--對特定的請求進行攔截-->
        <mvc:interceptor>
            <mapping path="/kfc/brands/brand1/*"/>
            <beans: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庐椒、攔截器進行攔截校驗

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

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

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

<mvc:interceptors>  

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

<!-- 登陸認證攔截器 -->  

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

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

    //用于身份認證宙搬,身份授權  

    //比如身份認證,如果認證通過表示當前用戶沒有登陸士鸥,需要此方法攔截不再往下執(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í)行到這里時表示用戶身份需要認證脚仔,跳轉(zhuǎn)到登陸頁面  

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

        return false;  

    }  

    //進入Handler方法之后鲤脏,返回modelAndView之前執(zhí)行  

    //應用場景從modelAndView出發(fā):將公用的模型數(shù)據(jù)(比如菜單導航)在這里傳到視圖猎醇,也可以在這里統(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í)行此方法  

    //應用場景:統(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進行用戶身份驗證  

        // 在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";  

    }  

}  

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末贡蓖,一起剝皮案震驚了整個濱河市煌茬,隨后出現(xiàn)的幾起案子坛善,更是在濱河造成了極大的恐慌剔交,老刑警劉巖改衩,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件葫督,死亡現(xiàn)場離奇詭異,居然都是意外死亡冯乘,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進店門领追,熙熙樓的掌柜王于貴愁眉苦臉地迎上來绒窑,“玉大人些膨,你說我怎么就攤上這事⊥莅ィ” “怎么了噩峦?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長凭涂。 經(jīng)常有香客問我,道長贴妻,這世上最難降的妖魔是什么切油? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮名惩,結(jié)果婚禮上白翻,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好滤馍,可當我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布岛琼。 她就那樣靜靜地躺著,像睡著了一般巢株。 火紅的嫁衣襯著肌膚如雪困檩。 梳的紋絲不亂的頭發(fā)上糟趾,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天,我揣著相機與錄音劫笙,去河邊找鬼栋盹。 笑死,一個胖子當著我的面吹牛怎茫,可吹牛的內(nèi)容都是我干的圃验。 我是一名探鬼主播摊聋,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼悲立,長吁一口氣:“原來是場噩夢啊……” “哼原献!你這毒婦竟也來了讲仰?” 一聲冷哼從身側(cè)響起趁矾,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤斑粱,失蹤者是張志新(化名)和其女友劉穎咒锻,沒想到半個月后思灌,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體耗跛,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡姜凄,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了盘寡。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片楚殿。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖匣缘,靈堂內(nèi)的尸體忽然破棺而出柑爸,到底是詐尸還是另有隱情,我是刑警寧澤谒亦,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布切揭,位于F島的核電站狞甚,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏廓旬。R本人自食惡果不足惜哼审,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望孕豹。 院中可真熱鬧涩盾,春花似錦、人聲如沸励背。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽椅野。三九已至终畅,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間竟闪,已是汗流浹背离福。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留炼蛤,地道東北人妖爷。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像理朋,于是被迫代替她去往敵國和親絮识。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,512評論 2 359

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理嗽上,服務發(fā)現(xiàn)次舌,斷路器,智...
    卡卡羅2017閱讀 134,701評論 18 139
  • hello springmvc 什么是Spring MVC?Spring MVC 為展現(xiàn)層提供的基于 MVC 設計...
    __豆約翰__閱讀 1,057評論 0 1
  • Jar包下載 commons-logging-1.2-bin.zip springframework 創(chuàng)建工程 i...
    Mr丶sorrow閱讀 357評論 0 0
  • 善建者不拔兽愤,善抱者不脫彼念,子孫以祭祀不輟。 修之於身浅萧,其德乃真逐沙; 修之於家,其德乃餘洼畅; 修之於鄉(xiāng)吩案,其德乃長; 修之於...
    一曲廣陵散閱讀 225評論 0 0
  • 文/意磬 [37]破碎 驚喜變驚嚇帝簇,任誰也無法忍受徘郭。這場本該圓滿的滿月酒靠益,現(xiàn)在成了瞿子鎮(zhèn)人酒足飯飽后的談資。 “志...
    意磬閱讀 842評論 33 39