激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - SpringMVC 實(shí)現(xiàn)用戶登錄實(shí)例代碼

SpringMVC 實(shí)現(xiàn)用戶登錄實(shí)例代碼

2020-08-13 12:10JAVA代碼網(wǎng) Java教程

這篇文章主要介紹了SpringMVC 實(shí)現(xiàn)用戶登錄實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下

SpringMVC的一個(gè)登陸小案例

準(zhǔn)備工作

  1. 創(chuàng)建一個(gè)Dynamic Web Project(本人是Eclipse)
  2. 添加相關(guān)的jar包,構(gòu)建路徑
  3. 創(chuàng)建springMVC-servlet.xml,及完善web.xml
  4. 創(chuàng)建代碼邏輯

目錄結(jié)構(gòu)如下

對(duì)于新手而言,有一個(gè)項(xiàng)目的完整的目錄結(jié)構(gòu)是多么幸福的一件事啊。

目錄結(jié)構(gòu)

SpringMVC 實(shí)現(xiàn)用戶登錄實(shí)例代碼

個(gè)人建議:注意其中的springMVC-servlet.xml的位置。以及源代碼包的名稱。

代碼實(shí)戰(zhàn)

首先是大管家,web.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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">
  <display-name>SpringTest</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
 
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.spring</url-pattern>
  </servlet-mapping>
 
 
</web-app>

然后是小管家springMVC-servlet.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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:p="http://www.springframework.org/schema/p"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
 
  <!-- 最簡單的配置,讓Spring自己去探索-->
  <context:component-scan base-package="controller"></context:component-scan>
 
 
</beans>

再就是一個(gè)登陸界面了,login.jsp:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ 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>登陸界面</title>
</head>
<body>
 
  <form action="login.spring" method="post">
    username:<input type="text" name="username"><br /> Password:<input
      type="password" name="password"><br /> <input type="submit"
      value="登陸">
  </form>
</body>
</html>

login.jsp對(duì)應(yīng)的那個(gè)action就是要進(jìn)行處理的后臺(tái)頁面,也就是我們的Login.Java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
 
@Controller // @Controller 代表本Java類是controller控制層
public class Login {
 
  /**
   * @RequestParam注解的作用是:根據(jù)參數(shù)名從URL中取得參數(shù)值
   * @param username
   *      用戶名,一定要對(duì)應(yīng)著表單的name才行
   * @param password
   *      用戶密碼,也應(yīng)該對(duì)應(yīng)表單的數(shù)據(jù)項(xiàng)
   * @param model
   *      一個(gè)域?qū)ο螅捎糜诖鎯?chǔ)數(shù)據(jù)值
   * @return
   */
  @RequestMapping("/login") // @RequestMapping 注解可以用指定的URL路徑訪問本控制層
  public String login(@RequestParam("username") String username, @RequestParam("password") String password,
      Model model) {
 
    if (username.equals("admin") && password.equals("admin")) {
      model.addAttribute("username", username);
      return "ok.jsp";
    } else {
      model.addAttribute("username", username);
      return "no.jsp";
    }
  }
 
}

最后就是ok.jsp和no.jsp了:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<%@ 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 here</title>
</head>
<body>
<font color="green">${username } </font>歡迎你!
 
</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 here</title>
</head>
<body>
 
  <font color="red">Sorry</font>,沒有${username }這個(gè)用戶!
  <br />
  <a href="login.jsp" rel="external nofollow" >重試一下!</a>
 
</body>
</html>

測(cè)試

在你的瀏覽器上輸入http://localhost:8080/SpringTest/login.jsp

然后就可以對(duì)代碼進(jìn)行測(cè)試了。本人親測(cè)好用,這里就不再貼圖了。

總結(jié)

  1. 在web.xml中配置DispatcherServlet核心控制器
  2. 在WEB-INF文件夾中創(chuàng)建springMVC-servlet.xml配置文件
  3. @Controller、@RequestMapping、@RequestParam以及Model域?qū)ο蟮鹊氖褂?/li>
  4. 表單以post方式,或者使用get方式都是可以的

下面是注解的小技巧:

@Controller就是對(duì)應(yīng)于springMVC-servlet.xml中的

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

原文鏈接:http://blog.csdn.net/marksinoberg/article/details/51482079

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本在线高清 | 91色琪琪电影亚洲精品久久 | 一区二区久久电影 | 毛片网站视频 | 日本aaaa片毛片免费观蜜桃 | 国产精品成人久久久久a级 av电影在线免费 | 狠狠干天天 | 中文字幕在线网站 | 91看片在线观看视频 | 久久精精品 | 黄色久| 久久密 | 日本在线免费观看视频 | 国产精品一区二区x88av | 精国产品一区二区三区四季综 | 日韩黄色片免费看 | 国产一级在线看 | 欧美成人综合视频 | 久久成人国产精品 | 精品国产一区二区三区久久久蜜 | 夜夜b| 国产精品视频亚洲 | 精品一区二区三区在线播放 | 免费在线观看成年人视频 | 国产成人在线观看免费 | 日韩毛片在线看 | 成人福利视频导航 | 亚洲国产精品久久久久婷婷老年 | 97视频一二区 | 久久免费视频精品 | 美女在线视频一区二区 | 日韩视频一区 | 精品国产乱码久久久久久久 | 热@国产| 鲁人人人鲁人人鲁精品 | 午夜国产成人 | 天天看成人免费毛片视频 | 午夜精品成人 | 亚洲成在人 | 亚洲综合无码一区二区 | 羞羞视频免费网站入口 |