最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2017年02月19日 09:17:20   投稿:lqh  
這篇文章主要介紹了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)

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

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

首先是大管家,web.xml:

<?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:

<?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:

<%@ 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:

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了:

<%@ 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>

測試

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

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

總結(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ì)本站的支持!

相關(guān)文章

  • springboot中@ConfigurationProperties無效果的解決方法

    springboot中@ConfigurationProperties無效果的解決方法

    本文主要介紹了springboot中@ConfigurationProperties無效果,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • springboot添加多數(shù)據(jù)源的方法實(shí)例教程

    springboot添加多數(shù)據(jù)源的方法實(shí)例教程

    這篇文章主要給大家介紹了關(guān)于springboot添加多數(shù)據(jù)源方法的相關(guān)資料,在實(shí)際開發(fā)中經(jīng)??赡苡龅皆谝粋€(gè)應(yīng)用中可能要訪問多個(gè)數(shù)據(jù)庫多的情況,需要的朋友可以參考下
    2023-09-09
  • java實(shí)現(xiàn)播放背景音樂

    java實(shí)現(xiàn)播放背景音樂

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)播放背景音樂,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • java實(shí)現(xiàn)郵件發(fā)送詳解

    java實(shí)現(xiàn)郵件發(fā)送詳解

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)郵件發(fā)送示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • java9遷移注意問題總結(jié)

    java9遷移注意問題總結(jié)

    本篇文章給大家詳細(xì)整理了java9遷移注意的問題,希望我們整理的內(nèi)容能夠幫助到大家。
    2018-02-02
  • Javascript和Java語言有什么關(guān)系?兩種語言間的異同比較

    Javascript和Java語言有什么關(guān)系?兩種語言間的異同比較

    雖然Javascript與Java有緊密的聯(lián)系,但卻是兩個(gè)公司開發(fā)的不同的兩個(gè)產(chǎn)品。那么js和java有什么關(guān)系,兩種語言的不同點(diǎn)是什么呢?介于這兩個(gè)問題,小編一起給大家解答下
    2016-09-09
  • 基于java實(shí)現(xiàn)簡單發(fā)紅包功能

    基于java實(shí)現(xiàn)簡單發(fā)紅包功能

    這篇文章主要為大家詳細(xì)介紹了基于java實(shí)現(xiàn)簡單發(fā)紅包功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • Java獲取隨機(jī)數(shù)的3種方法

    Java獲取隨機(jī)數(shù)的3種方法

    本篇文章主要介紹了Java獲取隨機(jī)數(shù)的3種方法,現(xiàn)在分享給大家,也給大家做個(gè)參考,感興趣的小伙伴們可以參考一下。
    2016-11-11
  • Eclipse中Properties和yml配置文件注釋亂碼的解決

    Eclipse中Properties和yml配置文件注釋亂碼的解決

    這篇文章主要介紹了Eclipse中Properties和yml配置文件注釋亂碼的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • spring+maven實(shí)現(xiàn)郵件發(fā)送

    spring+maven實(shí)現(xiàn)郵件發(fā)送

    這篇文章主要為大家詳細(xì)介紹了spring+maven實(shí)現(xiàn)郵件發(fā)送,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07

最新評(píng)論

玛曲县| 昆明市| 白沙| 甘德县| 朝阳区| 霍城县| 华蓥市| 上虞市| 克山县| 彭泽县| 望奎县| 屏山县| 东乌| 泰宁县| 从江县| 南昌县| 南投县| 鹤峰县| 淅川县| 建始县| 福泉市| 定日县| 子长县| 台东县| 泸州市| 五家渠市| 尉犁县| 沙坪坝区| 罗城| 泸定县| 进贤县| 五家渠市| 赫章县| 孝感市| 庆阳市| 姚安县| 广昌县| 纳雍县| 车致| 京山县| 彭山县|