SpringMVC(二)

SpringMVC註解方式
    1.經常使用註解:
        名稱                說明
        @Controller        指明某個類是一個Controller
        @RequestMapping    請求路徑的映射,能夠配置在類或者方法上面
        @RequestParam    配置在方法的參數前面,指明從request中獲取指定的值並注入到參數中
        @PathVariable    指明從請求的地址的中獲取配置項來注入到方法的參數中
        
    2.使用註解完成用戶登陸
        a.在所須要的Controller中標註@Controller
        b.在dispatcherServlet-servlet.xml配置
            1)配置視圖解析器
            2)配置須要掃描的包
            3)啓動SpringMVC註解掃描
            
    3.REST風格
        a.Representational State Transfer表述性狀態轉移
        b.傳統的查、改、刪的URL與REST風格的增刪改URL對比
            /userview.action?id=12    <-->   /user/12
            /userdelete.action?id=12  <-->   /user/delete/12
            /userupdate.action?id=12  <-->   /user/update/12
        c.請求方式
            GET、POST、DELETE、PUT
        d.他強調的是一個資源能夠對應多種視圖
        
    4.JSR-303驗證
        a.使用註解完成實體類的驗證
            1)須要導入hibernate-validator驗證所需jar包
            2)在實體類上配置驗證註解
                @NotEmpty
                @Range
                @Length
                @Pattern
                @Valid
        b.DispatcherServlet
            1)Handler
                @Controller
                @RequestMapping(value、method、params)
                @PathVariable
                @RequestParam
                @ResponseBody
            2)方法的返回值
                ModelAndView
                View
                Model
                String
                Object
                void

訪問靜態文件
    在SpringMVC中配置了攔截地址爲 / 時,攔截全部請求,靜態資源(圖片、外部樣式、外部JS)都會被攔截
    解決方案:使用<mvc:resources/>排除靜態資源攔截

返回JSON數據
    1.步驟:
        a.導入jar包:jackson-all-1.9.11.jar
        b.@RequestMapping(value="/{username}",method=RequestMethod.GET,params="json")
        c.@ResponseBody
        
範例:
1.entity包
a.Userjavascript

 1 package com.Elastic.SpringMVCDemo2.ivy.entity;
 2 
 3 import java.io.Serializable;
 4 
 5 public class User implements Serializable {
 6     private String name;
 7     private String pass;
 8 
 9     public String getName() {
10         return name;
11     }
12 
13     public void setName(String name) {
14         this.name = name;
15     }
16 
17     public String getPass() {
18         return pass;
19     }
20 
21     public void setPass(String pass) {
22         this.pass = pass;
23     }
24 }


2.controller包
a1.UserControllercss

 1 package com.Elastic.SpringMVCDemo2.ivy.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RequestMethod;
 6 import org.springframework.web.bind.annotation.RequestParam;
 7 import org.springframework.web.servlet.ModelAndView;
 8 
 9 import com.Elastic.SpringMVCDemo2.ivy.entity.User;
10 
11 @Controller
12 public class UserController {
13     /**
14      * 訪問登陸頁面
15      * @return
16      */
17     @RequestMapping("/login")
18     public ModelAndView login() {
19         return new ModelAndView("login");
20     }
21     
22     @RequestMapping("/doLogin")
23     public ModelAndView doLogin(User user, String code) {
24         System.out.println(user.getName());
25         System.out.println(code);
26         return new ModelAndView("index");
27     }
28 }


a2.jsp中name與controller中參數名不一樣時 -- UserControllerhtml

 1 package com.Elastic.SpringMVCDemo2.ivy.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RequestMethod;
 6 import org.springframework.web.bind.annotation.RequestParam;
 7 import org.springframework.web.servlet.ModelAndView;
 8 
 9 import com.Elastic.SpringMVCDemo2.ivy.entity.User;
10 
11 @Controller
12 public class UserController {
13     /**
14      * 訪問登陸頁面
15      * @return
16      */
17     @RequestMapping("/login")
18     public ModelAndView login() {
19         return new ModelAndView("login");
20     }
21     
22     @RequestMapping("/doLogin")
23     public ModelAndView doLogin(@RequestParam(value="name1", required=false) String name1, String pass) {
24         System.out.println(name1);
25         return new ModelAndView("index");
26     }
27     
28 }


a3.required=false和defaultValue="asd"屬性經常使用在分頁 -- UserControllerjava

 1 package com.Elastic.SpringMVCDemo2.ivy.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RequestMethod;
 6 import org.springframework.web.bind.annotation.RequestParam;
 7 import org.springframework.web.servlet.ModelAndView;
 8 
 9 import com.Elastic.SpringMVCDemo2.ivy.entity.User;
10 
11 @Controller
12 public class UserController {
13     /**
14      * 訪問登陸頁面
15      * @return
16      */
17     @RequestMapping("/login")
18     public ModelAndView login() {
19         return new ModelAndView("login");
20     }
21     
22     @RequestMapping("/doLogin")
23     public ModelAndView doLogin(@RequestParam(defaultValue="asd", required=false) String name1, String pass) {
24         System.out.println(name1);
25         return new ModelAndView("index");
26     }
27     
28 }


a4.方法重載 -- UserControllerjquery

 1 package com.Elastic.SpringMVCDemo2.ivy.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RequestMethod;
 6 import org.springframework.web.bind.annotation.RequestParam;
 7 import org.springframework.web.servlet.ModelAndView;
 8 
 9 import com.Elastic.SpringMVCDemo2.ivy.entity.User;
10 
11 @Controller
12 public class UserController {
13     /**
14      * 訪問登陸頁面
15      * @return
16      */
17     @RequestMapping(value="/login",method=RequestMethod.GET)
18     public ModelAndView login() {
19         return new ModelAndView("login");
20     }
21     
22     @RequestMapping(value="/login",method=RequestMethod.POST)
23     public ModelAndView login(User user, String code) {
24         System.out.println(user.getName());
25         System.out.println(code);
26         return new ModelAndView("index");
27     }
28 }


a5.增刪查改 + 多參 -- UserControllerweb

 1 package com.Elastic.SpringMVCDemo2.ivy.controller;
 2 
 3 import javax.websocket.server.PathParam;
 4 
 5 import org.springframework.stereotype.Controller;
 6 import org.springframework.web.bind.annotation.PathVariable;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.RequestMethod;
 9 import org.springframework.web.bind.annotation.RequestParam;
10 import org.springframework.web.servlet.ModelAndView;
11 
12 import com.Elastic.SpringMVCDemo2.ivy.entity.User;
13 
14 @Controller
15 public class UserController {
16     @RequestMapping(value="/delete/{id}")
17     public ModelAndView delete(@PathVariable(value="id") String name) {
18         System.out.println("執行刪除");
19         System.out.println(name);
20         return new ModelAndView("index");
21     }
22 
23     /*@RequestMapping(value="/delete/{id}/{name}")
24     public ModelAndView delete(@PathVariable String id,@PathVariable String name) {
25         System.out.println("執行刪除");
26         System.out.println(id);
27         System.out.println(name);
28         return new ModelAndView("index");
29     }*/
30     
31     @RequestMapping(value="/delete/{id}/{name}")
32     public ModelAndView search(String name,String sex,int age) {
33         System.out.println("執行搜索");
34         return new ModelAndView("list");
35     }
36 }


a6.頁面顯示信息 -- UserControllerajax

 1 package com.Elastic.SpringMVCDemo2.ivy.controller;
 2 
 3 import javax.websocket.server.PathParam;
 4 
 5 import org.springframework.stereotype.Controller;
 6 import org.springframework.ui.Model;
 7 import org.springframework.ui.ModelMap;
 8 import org.springframework.web.bind.annotation.PathVariable;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.RequestMethod;
11 import org.springframework.web.bind.annotation.RequestParam;
12 import org.springframework.web.servlet.ModelAndView;
13 
14 import com.Elastic.SpringMVCDemo2.ivy.entity.User;
15 
16 @Controller
17 public class UserController {
18     /**
19      * 訪問登陸頁面
20      * @return
21      */
22      //方法1
23     /*@RequestMapping("/login")
24     public ModelAndView login() {
25         return new ModelAndView("login","msg","歡迎使用xxx系統");
26     }*/
27     
28     //方法2
29     /*@RequestMapping(value="/login",method=RequestMethod.GET)
30     public String login(Model model) {
31         model.addAttribute("msg", "歡迎使用xxx系統");
32         return "login";
33     }*/
34     
35     //方法3
36     @RequestMapping(value="/login",method=RequestMethod.GET)
37     public String login(ModelMap modelMap) {
38         modelMap.put("msg", "歡迎使用xxx系統");
39         return "login";
40     }
41 }


a7.重定向 -- UserControllerspring

 1 package com.Elastic.SpringMVCDemo2.ivy.controller;
 2 
 3 import javax.websocket.server.PathParam;
 4 
 5 import org.springframework.stereotype.Controller;
 6 import org.springframework.ui.Model;
 7 import org.springframework.ui.ModelMap;
 8 import org.springframework.web.bind.annotation.PathVariable;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.RequestMethod;
11 import org.springframework.web.bind.annotation.RequestParam;
12 import org.springframework.web.servlet.ModelAndView;
13 
14 import com.Elastic.SpringMVCDemo2.ivy.entity.User;
15 
16 @Controller
17 public class UserController {
18     @RequestMapping(value="/login",method=RequestMethod.GET)
19     public String login(ModelMap modelMap) {
20         modelMap.put("msg", "歡迎使用xxx系統");
21 //        邏輯視圖名稱
22 //        return "login";
23 //        重定向:URL地址
24         return "redirect:/index";
25     }
26 }


a8.完整版 -- UserControllerchrome

 1 package com.Elastic.SpringMVCDemo2.ivy.controller;
 2 
 3 import javax.validation.Valid;
 4 import javax.websocket.server.PathParam;
 5 
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.ui.Model;
 8 import org.springframework.ui.ModelMap;
 9 import org.springframework.validation.BindingResult;
10 import org.springframework.web.bind.annotation.ModelAttribute;
11 import org.springframework.web.bind.annotation.PathVariable;
12 import org.springframework.web.bind.annotation.RequestMapping;
13 import org.springframework.web.bind.annotation.RequestMethod;
14 import org.springframework.web.bind.annotation.RequestParam;
15 import org.springframework.web.servlet.ModelAndView;
16 
17 import com.Elastic.SpringMVCDemo2.ivy.entity.User;
18 
19 @Controller
20 public class UserController {
21     @RequestMapping(value="/login",method=RequestMethod.GET)
22     public String login(ModelMap modelMap) {
23         modelMap.put("msg", "歡迎使用xxx系統");
24 //        邏輯視圖名稱
25         return "login";
26 //        重定向:URL地址
27 //        return "redirect:/index";
28     }
29     
30     @RequestMapping(value="/login",method=RequestMethod.POST)
31     //@ModelAttribute("loginUser")
32     public ModelAndView login(@Valid User user, BindingResult bindingResult, String code, ModelMap modelMap) {
33         if (null == code || code.trim().length() == 0) {
34             modelMap.put("codeError", "驗證碼不能爲空");
35         }
36         
37         if (bindingResult.hasErrors()) {
38             modelMap.addAllAttributes(bindingResult.getModel());
39             System.out.println("有錯誤");
40             System.out.println(bindingResult.getModel());
41             return new ModelAndView("login",modelMap);
42         }
43         return new ModelAndView("index");
44     }
45 }


a9.json -- UserControllerjson

 1 package com.Elastic.SpringMVCDemo2.ivy.controller;
 2 
 3 import javax.validation.Valid;
 4 import javax.websocket.server.PathParam;
 5 
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.ui.Model;
 8 import org.springframework.ui.ModelMap;
 9 import org.springframework.validation.BindingResult;
10 import org.springframework.web.bind.annotation.ModelAttribute;
11 import org.springframework.web.bind.annotation.PathVariable;
12 import org.springframework.web.bind.annotation.RequestMapping;
13 import org.springframework.web.bind.annotation.RequestMethod;
14 import org.springframework.web.bind.annotation.RequestParam;
15 import org.springframework.web.servlet.ModelAndView;
16 
17 import com.Elastic.SpringMVCDemo2.ivy.entity.User;
18 
19 @Controller
20 public class UserController {
21     @RequestMapping(value="/login",method=RequestMethod.GET)
22     public String login(ModelMap modelMap) {
23         modelMap.put("msg", "歡迎使用xxx系統");
24 //        邏輯視圖名稱
25         return "login";
26 //        重定向:URL地址
27 //        return "redirect:/index";
28     }
29     
30     @RequestMapping(value="/login",method=RequestMethod.POST)
31     //@ModelAttribute("loginUser")
32     public ModelAndView login(@Valid User user, BindingResult bindingResult, String code, ModelMap modelMap) {
33         if (null == code || code.trim().length() == 0) {
34             modelMap.put("codeError", "驗證碼不能爲空");
35         }
36         
37         if (bindingResult.hasErrors()) {
38             modelMap.addAllAttributes(bindingResult.getModel());
39             System.out.println("有錯誤");
40             System.out.println(bindingResult.getModel());
41             return new ModelAndView("login",modelMap);
42         }
43         return new ModelAndView("index");
44     }
45     
46     @RequestMapping("/ajax/login")
47     @ResponseBody
48     public Object ajaxLogin(){
49         Map<String, Object> result = new HashMap<String, Object>();
50         result.put("name", "admin");
51         result.put("pass", "123456");
52         result.put("success", true);
53         return result;
54     }
55 }


b.IndexController

 1 package com.Elastic.SpringMVCDemo2.ivy.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 
 6 @Controller
 7 public class IndexController {
 8     @RequestMapping("/index")
 9     public String index() {
10         return "index";
11     }
12 }


3.Spring配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:tx="http://www.springframework.org/schema/tx"
 6     xmlns:context="http://www.springframework.org/schema/context"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans
 8         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
 9         http://www.springframework.org/schema/aop
10         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
11         http://www.springframework.org/schema/tx
12         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
13         http://www.springframework.org/schema/context
14         http://www.springframework.org/schema/context/spring-context-3.2.xsd">
15 
16 
17 </beans>


4.SpringMVC配置文件
a1.SpringMVC-servlet.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
 8         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
 9     <!-- 配置須要掃描註解的包 -->
10     <context:component-scan base-package="com.Elastic.SpringMVCDemo2.ivy.controller"></context:component-scan>
11     
12     <!-- 開啓SpringMVC註解掃描 -->
13     <mvc:annotation-driven></mvc:annotation-driven>
14     
15     <!-- 配置視圖解析器 -->
16     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
17         <property name="prefix" value="/WEB-INF/views/"></property>
18         <property name="suffix" value=".jsp"></property>
19     </bean>
20 </beans>


a2.SpringMVC-servlet.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
 8         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
 9     <!-- 配置須要掃描註解的包 -->
10     <context:component-scan base-package="com.Elastic.SpringMVCDemo2.ivy.controller"></context:component-scan>
11     
12     <!-- 開啓SpringMVC註解掃描 -->
13     <mvc:annotation-driven></mvc:annotation-driven>
14     
15     <!-- 配置視圖解析器 -->
16     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
17         <property name="prefix" value="/WEB-INF/views/"></property>
18         <property name="suffix" value=".jsp"></property>
19     </bean>
20     
21     <!-- 配置靜態資源(圖片、CSS、JS) -->
22     <mvc:resources location="/css/*" mapping="/css/**"/>
23     <mvc:resources location="/js/*" mapping="/js/**"/>
24 </beans>


5.web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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_2_5.xsd" id="WebApp_ID" version="2.5">
 3   <display-name>SpringMVCDemo1_ivy</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>
12  
13   <!-- 指定Spring的配置文件 -->
14   <context-param>
15       <param-name>contextConfigLocation</param-name>
16       <param-value>classpath:applicationContext.xml</param-value>
17   </context-param>
18  
19   <!-- 配置Spring的監聽器 -->
20   <listener>
21       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
22   </listener>
23  
24   <!-- 配置SpringMVC的攔截器 -->
25   <servlet>
26       <servlet-name>SpringMVC</servlet-name>
27       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
28       <init-param>
29           <param-name>contextConfigLocation</param-name>
30           <param-value>classpath:SpringMVC-servlet.xml</param-value>
31       </init-param>
32       <load-on-startup>1</load-on-startup>
33   </servlet>
34   <servlet-mapping>
35       <servlet-name>SpringMVC</servlet-name>
36       <url-pattern>/</url-pattern>
37   </servlet-mapping>
38  
39 </web-app>

 

6.jsp
a.login.jsp

 1 <%--引入JSP頁面PAGE指令 --%>
 2 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 3 <%-- 引入JSTL標籤指令 --%>
 4 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 5 <!DOCTYPE html>
 6 <html language="zh-CN">
 7 <head>
 8     <meta charset="utf-8">
 9     <!-- 設置瀏覽器渲染的引擎  -->
10     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
11     <!-- 設置支持移動設備  -->
12     <meta name="viewport" content="width=device-width, initial-scale=1">
13     <title>登陸</title>
14     <!-- 引入bootstrap的樣式 -->
15     <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/bootstrap.min.css">
16 </head>
17 <body>
18     <div class="container">
19         <div class="panel panel-primary center-block">
20             <div class="panel-heading">登陸</div>
21             <div class="panel-body">
22                 <form action="" method="post">
23                     <div class="form-group">
24                         <label class="col-md-3 control-label" for="loginName">用戶名:</label>
25                         <div class="col-md-6" >
26                             <input class="form-control" id="loginName" name="name" type="text" autocomplete="off"/>
27                         </div>
28                     </div>
29                     <div class="form-group">
30                         <label class="col-md-3 control-label" for="loginPass">密碼:</label>
31                         <div class="col-md-6" >
32                             <input class="form-control" id="loginPass" name="pass" type="password"/>
33                         </div>
34                     </div>
35                     <div class="form-group">
36                         <label class="col-md-3 control-label" for="code">驗證碼:</label>
37                         <div class="col-md-6" >
38                             <input class="form-control" id="code" name="code" type="text"/>
39                         </div>
40                     </div>
41                     
42                     <div class="form-group">
43                         <div class="col-md-offset-3 col-md-6">
44                             <input class="btn btn-primary btn-block" type="submit" value="登陸" />
45                         </div>
46                     </div>
47                 </form>
48             </div>
49         </div>
50     </div>
51     
52     <!-- 引入JS的樣式  -->
53     <script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery-2.2.4.js"></script>
54     <script type="text/javascript" src="<%=request.getContextPath()%>/js/bootstrap.min.js"></script>
55     
56 </body>
57 </html>


a2.頁面顯示信息 -- login.jsp

 1 <%--引入JSP頁面PAGE指令 --%>
 2 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 3 <%-- 引入JSTL標籤指令 --%>
 4 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 5 <!DOCTYPE html>
 6 <html language="zh-CN">
 7 <head>
 8     <meta charset="utf-8">
 9     <!-- 設置瀏覽器渲染的引擎  -->
10     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
11     <!-- 設置支持移動設備  -->
12     <meta name="viewport" content="width=device-width, initial-scale=1">
13     <title>登陸</title>
14     <!-- 引入bootstrap的樣式 -->
15     <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/bootstrap.min.css">
16 </head>
17 <body>
18     <div class="container">
19         <div class="panel panel-primary center-block">
20             <div class="panel-heading">${msg }</div>
21             <div class="panel-body">
22                 <!-- <fm:form action="user/login" method="post"> -->
23                 <form action="" method="post">
24                     <div class="form-group">
25                         <label class="col-md-3 control-label" for="loginName">用戶名:</label>
26                         <div class="col-md-6" >
27                             <input class="form-control" id="loginName" name="name" type="text" autocomplete="off"/>
28                         </div>
29                         <div class="col-md-3">
30                             <!-- <fm:errors path="user.name"></fm:errors> -->
31                         </div>
32                     </div>
33                     <div class="form-group">
34                         <label class="col-md-3 control-label" for="loginPass">密碼:</label>
35                         <div class="col-md-6" >
36                             <input class="form-control" id="loginPass" name="pass" type="password"/>
37                         </div>
38                         <div class="col-md-3">
39                             <!-- <fm:errors path="user.pass"></fm:errors> -->
40                         </div>
41                     </div>
42                     <div class="form-group">
43                         <label class="col-md-3 control-label" for="code">驗證碼:</label>
44                         <div class="col-md-6" >
45                             <input class="form-control" id="code" name="code" type="text"/>
46                         </div>
47                         <div class="col-md-3">
48                         <!--     <fm:errors path="code"></fm:errors> -->
49                         </div>
50                     </div>
51                     
52                     <div class="form-group">
53                         <div class="col-md-offset-3 col-md-6">
54                             <input class="btn btn-primary btn-block" type="submit" value="登陸" />
55                         </div>
56                     </div>
57                 <!-- </fm:form> -->
58                 </form>
59             </div>
60         </div>
61     </div>
62     
63     <!-- 引入JS的樣式  -->
64     <script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery-2.2.4.js"></script>
65     <script type="text/javascript" src="<%=request.getContextPath()%>/js/bootstrap.min.js"></script>
66     
67 </body>
68 </html>


a3.Spring標籤 -- login.jsp

 1 <%--引入JSP頁面PAGE指令 --%>
 2 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 3 <%-- 引入JSTL標籤指令 --%>
 4 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 5 <%@ taglib uri="http://www.springframework.org/tags/form" prefix="fm"%>
 6 <!DOCTYPE html>
 7 <html language="zh-CN">
 8 <head>
 9     <meta charset="utf-8">
10     <!-- 設置瀏覽器渲染的引擎  -->
11     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
12     <!-- 設置支持移動設備  -->
13     <meta name="viewport" content="width=device-width, initial-scale=1">
14     <title>登陸</title>
15     <!-- 引入bootstrap的樣式 -->
16     <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/css/bootstrap.min.css">
17 </head>
18 <body>
19     <div class="container">
20         <div class="panel panel-primary center-block">
21             <div class="panel-heading">${msg }</div>
22             <div class="panel-body">
23                 <fm:form action="" method="post" modelAttribute="user">
24                     <div class="form-group">
25                         <label class="col-md-3 control-label" for="loginName">用戶名:</label>
26                         <div class="col-md-6" >
27                             <input class="form-control" id="loginName" name="name" type="text" autocomplete="off"/>
28                         </div>
29                         <div class="col-md-3">
30                             <fm:errors path="name"></fm:errors>
31                         </div>
32                     </div>
33                     <div class="form-group">
34                         <label class="col-md-3 control-label" for="loginPass">密碼:</label>
35                         <div class="col-md-6" >
36                             <input class="form-control" id="loginPass" name="pass" type="password"/>
37                         </div>
38                         <div class="col-md-3">
39                             <fm:errors path="pass"></fm:errors>
40                         </div>
41                     </div>
42                     <div class="form-group">
43                         <label class="col-md-3 control-label" for="code">驗證碼:</label>
44                         <div class="col-md-6" >
45                             <input class="form-control" id="code" name="code" type="text"/>
46                         </div>
47                         <div class="col-md-3">
48                             <fm:errors path="code"></fm:errors>
49                         </div>
50                     </div>
51                     
52                     <div class="form-group">
53                         <div class="col-md-offset-3 col-md-6">
54                             <input class="btn btn-primary btn-block" type="submit" value="登陸" />
55                         </div>
56                     </div>
57                 </fm:form>
58             </div>
59         </div>
60     </div>
61     
62     <!-- 引入JS的樣式  -->
63     <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-2.2.4.js"></script>
64     <script type="text/javascript" src="${pageContext.request.contextPath }/js/bootstrap.min.js"></script>
65     
66 </body>
67 </html>


a4.完整版 -- login.jsp

 1 <%--引入JSP頁面PAGE指令 --%>
 2 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 3 <%-- 引入JSTL標籤指令 --%>
 4 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 5 <%@ taglib uri="http://www.springframework.org/tags/form" prefix="fm"%>
 6 <!DOCTYPE html>
 7 <html language="zh-CN">
 8 <head>
 9     <meta charset="utf-8">
10     <!-- 設置瀏覽器渲染的引擎  -->
11     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
12     <!-- 設置支持移動設備  -->
13     <meta name="viewport" content="width=device-width, initial-scale=1">
14     <title>登陸</title>
15     <!-- 引入bootstrap的樣式 -->
16     <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/css/bootstrap.min.css">
17 </head>
18 <body>
19     <div class="container">
20         <div class="panel panel-primary center-block">
21             <div class="panel-heading">${msg }</div>
22             <div class="panel-body">
23                 <fm:form action="" method="post" modelAttribute="user">
24                     <div class="form-group">
25                         <label class="col-md-3 control-label" for="loginName">用戶名:</label>
26                         <div class="col-md-6" >
27                             <input class="form-control" id="loginName" name="name" type="text" autocomplete="off"/>
28                         </div>
29                         <div class="col-md-3">
30                             <fm:errors path="name"></fm:errors>
31                         </div>
32                     </div>
33                     <div class="form-group">
34                         <label class="col-md-3 control-label" for="loginPass">密碼:</label>
35                         <div class="col-md-6" >
36                             <input class="form-control" id="loginPass" name="pass" type="password"/>
37                         </div>
38                         <div class="col-md-3">
39                             <fm:errors path="pass"></fm:errors>
40                         </div>
41                     </div>
42                     <div class="form-group">
43                         <label class="col-md-3 control-label" for="code">驗證碼:</label>
44                         <div class="col-md-6" >
45                             <input class="form-control" id="code" name="code" type="text"/>
46                         </div>
47                         <div class="col-md-3">
48                             ${codeError }
49                         </div>
50                     </div>
51                     
52                     <div class="form-group">
53                         <div class="col-md-offset-3 col-md-6">
54                             <input class="btn btn-primary btn-block" type="submit" value="登陸" />
55                         </div>
56                     </div>
57                 </fm:form>
58             </div>
59         </div>
60     </div>
61     
62     <!-- 引入JS的樣式  -->
63     <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-2.2.4.js"></script>
64     <script type="text/javascript" src="${pageContext.request.contextPath }/js/bootstrap.min.js"></script>
65     
66 </body>
67 </html>


b.index.jsp

 1 <%--引入JSP頁面PAGE指令 --%>
 2 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 3 <%-- 引入JSTL標籤指令 --%>
 4 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 5 <!DOCTYPE html>
 6 <html language="zh-CN">
 7 <head>
 8     <meta charset="utf-8">
 9     <!-- 設置瀏覽器渲染的引擎  -->
10     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
11     <!-- 設置支持移動設備  -->
12     <meta name="viewport" content="width=device-width, initial-scale=1">
13     <title>首頁</title>
14     <!-- 引入bootstrap的樣式 -->
15     <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/bootstrap.min.css">
16 </head>
17 <body>
18     <div class="container">
19         <h1>首頁</h1>
20     </div>
21     
22     <!-- 引入JS的樣式  -->
23     <script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery-2.2.4.js"></script>
24     <script type="text/javascript" src="<%=request.getContextPath()%>/js/bootstrap.min.js"></script>
25     
26 </body>
27 </html>
相關文章
相關標籤/搜索