欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring MVC登錄注冊(cè)以及轉(zhuǎn)換json數(shù)據(jù)

 更新時(shí)間:2017年04月06日 14:17:02   作者:雨落秋垣  
本文主要介紹了Spring MVC登錄注冊(cè)以及轉(zhuǎn)換json數(shù)據(jù)的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧

項(xiàng)目結(jié)構(gòu);

代碼如下:

BookController

package com.mstf.controller;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.map.ObjectMapper;
import com.mstf.domain.Book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/json")
public class BookController {
 private static final Log logger = LogFactory.getLog(BookController.class);
 // @RequestMapping 根據(jù) json 數(shù)據(jù),轉(zhuǎn)換成對(duì)應(yīng)的 Object
 @RequestMapping(value="/testRequestBody")
 public void setJson(@RequestBody Book book,HttpServletResponse response) throws Exception {
  // ObjectMapper 類是 Jackson 庫(kù)的主要類。他提供一些功能將 Java 對(duì)象轉(zhuǎn)換成對(duì)應(yīng)的 JSON
  ObjectMapper mapper = new ObjectMapper();
  // 將 Book 對(duì)象轉(zhuǎn)換成 json 輸出
  logger.info(mapper.writeValueAsString(book));
  book.setAuthor("汪政");
  response.setContentType("text/html;charset=UTF-8");
  // 將 Book 對(duì)象轉(zhuǎn)換成 json 寫到客戶端
  response.getWriter().println(mapper.writeValueAsString(book));
 }
}

UserController

package com.mstf.controller;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.mstf.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

// Controller 注解用于指示該類是一個(gè)控制器,可以同時(shí)處理多個(gè)請(qǐng)求動(dòng)作
@Controller
// RequestMapping 可以用來(lái)注釋一個(gè)控制器類,此時(shí),所有方法都將映射為相對(duì)于類級(jí)別的請(qǐng)求,
// 表示該控制器處理所有的請(qǐng)求都被映射到 value屬性所指示的路徑下
@RequestMapping(value="/user")
public class UserController {
 // 靜態(tài) List<User> 集合,此處代替數(shù)據(jù)庫(kù)用來(lái)保存注冊(cè)的用戶信息
 private static List<User> userList;
 // UserController 類的構(gòu)造器,初始化 List<User> 集合
 public UserController() {
  super();
  userList = new ArrayList<User>();
 }
 // 靜態(tài)的日志類 LogFactory
 private static final Log logger = LogFactory.getLog(UserController.class);
 // 該方法映射的請(qǐng)求為 http://localhost:8080/context/user/register ,該方法支持GET請(qǐng)求
 @RequestMapping(value="/register",method=RequestMethod.GET)
 public String registerForm() {
  logger.info("register GET方法被調(diào)用...");
  // 跳轉(zhuǎn)到注冊(cè)頁(yè)面
  return "register";
 }
 // 該方法映射的請(qǐng)求支持 POST 請(qǐng)求
 @RequestMapping(value="/register",method=RequestMethod.POST)
 // 將請(qǐng)求中的 loginname 參數(shù)的值賦給 loginname 變量, password 和 username 同樣處理
 public String register(
   @RequestParam("loginName") String loginName,
   @RequestParam("passWord") String passWord,
   @RequestParam("userName") String userName) {
  logger.info("register POST方法被調(diào)用...");
  // 創(chuàng)建 User 對(duì)象
  User user = new User();
  user.setLoginName(loginName);
  user.setPassWord(passWord);
  user.setUserName(userName);
  // 模擬數(shù)據(jù)庫(kù)存儲(chǔ) User 信息
   userList.add(user);
  // 跳轉(zhuǎn)到登錄頁(yè)面
  return "login";
 }
 // 該方法映射的請(qǐng)求為 http://localhost:8080/RequestMappingTest/user/login
 @RequestMapping("/login")
 public String login(
   // 將請(qǐng)求中的 loginName 參數(shù)的值賦給 loginName 變量, passWord 同樣處理
   @RequestParam("loginName") String loginName,
   @RequestParam("passWord") String passWord,
   Model model) {
  logger.info("登錄名:"+loginName + " 密碼:" + passWord);
  // 到集合中查找用戶是否存在,此處用來(lái)模擬數(shù)據(jù)庫(kù)驗(yàn)證
  for(User user : userList){
   if(user.getLoginName().equals(loginName)
     && user.getPassWord().equals(passWord)){
    model.addAttribute("user",user);
    return "welcome";
   }
  }
  return "login";
 }
}

Book

package com.mstf.domain;
import java.io.Serializable;
public class Book implements Serializable {
 private static final long serialVersionUID = 1L;
 private int id;
 private String name;
 private String author;
 public Book() {
 }
 public Book(int id, String name, String author) {
  super();
  this.id = id;
  this.name = name;
  this.author = author;
 }
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getAuthor() {
  return author;
 }
 public void setAuthor(String author) {
  this.author = author;
 }
 @Override
 public String toString() {
  return "Book [id=" + id + ", name=" + name + ", author=" + author + "]";
 }
}

User

package com.mstf.domain;
import java.io.Serializable;
// 域?qū)ο?,?shí)現(xiàn)序列化接口
public class User implements Serializable {
 // 序列化
 private static final long serialVersionUID = 1L;
 // 私有字段
 private String loginName;
 private String userName;
 private String passWord;
 // 公共構(gòu)造器
 public User() {
  super();
 }
 // get/set 方法
 public String getLoginName() {
  return loginName;
 }
 public void setLoginName(String loginName) {
  this.loginName = loginName;
 }
 public String getUserName() {
  return userName;
 }
 public void setUserName(String userName) {
  this.userName = userName;
 }
 public String getPassWord() {
  return passWord;
 }
 public void setPassWord(String passWord) {
  this.passWord = passWord;
 }
}

springmvc-config.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:mvc="http://www.springframework.org/schema/mvc"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.2.xsd">

 <!-- spring可以自動(dòng)去掃描base-pack下面的包或者子包下面的java文件,
  如果掃描到有Spring的相關(guān)注解的類,則把這些類注冊(cè)為Spring的bean -->
 <context:component-scan base-package="com.mstf.controller"/>

 <!-- 設(shè)置配置方案 -->
 <mvc:annotation-driven/>
 <!-- 使用默認(rèn)的 servlet 來(lái)響應(yīng)靜態(tài)文件 -->
 <mvc:default-servlet-handler/>

 <!-- 視圖解析器 -->
  <bean id="viewResolver"
   class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <!-- 前綴 -->
  <property name="prefix">
   <value>/WEB-INF/jsp/</value>
  </property>
  <!-- 后綴 -->
  <property name="suffix">
   <value>.jsp</value>
  </property>
 </bean>
</beans>

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>
 <h3>登錄</h3>
 <br>
 <form action="login" method="post">
  <table>
   <tr>
    <td>
     <label>
      登錄名:
     </label>
    </td>
    <td>
     <input type="text" id="loginName" name="loginName">
    </td>
   </tr>
   <tr>
    <td>
     <label>
      密 碼:
     </label>
    </td>
    <td>
     <input type="password" id="passWord" name="passWord">
    </td>
   </tr>
   <tr>
    <td>
     <input id="submit" type="submit" value="登錄">
    </td>
   </tr>
  </table>
 </form>
</body>
</html>

register.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>注冊(cè)</title>
</head>
<body>
 <h3>注冊(cè)頁(yè)面</h3>
 <br>
 <form action="register" method="post">
  <table>
   <tr>
    <td>
     <label>
      登錄名:
     </label>
    </td>
    <td>
     <input type="text" id="loginName" name="loginName">
    </td>
   </tr>
   <tr>
    <td>
     <label>
      密 碼:
     </label>
    </td>
    <td>
     <input type="password" id="passWord" name="passWord">
    </td>
   </tr>
   <tr>
    <td>
     <label>
      姓 名:
     </label>
    </td>
    <td>
     <input type="text" id="userName" name="userName">
    </td>
   </tr>
   <tr>
    <td>
     <input id="submit" type="submit" value="注冊(cè)">
    </td>
   </tr>
  </table>
 </form>
</body>
</html>

welcome.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>
 <h3>歡迎[${requestScope.user.userName }]登錄</h3>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 version="3.0">
 <!-- 定義 Spring MVC 的前端控制器 -->
 <servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>
   org.springframework.web.servlet.DispatcherServlet
  </servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>
    /WEB-INF/config/springmvc-config.xml
   </param-value>
  </init-param>
  <load-on-startup>1</load-on-startup> 
 </servlet>
 <!-- 讓 Spring MVC 的前端控制器攔截所有請(qǐng)求 -->
 <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
 <!-- 亂碼過(guò)濾器 -->
 <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>
</web-app>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>測(cè)試接收J(rèn)SON格式的數(shù)據(jù)</title>
<script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="js/json2.js"></script>
<script type="text/javascript">
 $(document).ready(function(){
  testRequestBody();
 });
 function testRequestBody(){
  $.ajax("${pageContext.request.contextPath}/json/testRequestBody",// 發(fā)送請(qǐng)求的 URL 字符串。
    {
    dataType : "json", // 預(yù)期服務(wù)器返回的數(shù)據(jù)類型。
    type : "post", // 請(qǐng)求方式 POST 或 GET
    contentType:"application/json", // 發(fā)送信息至服務(wù)器時(shí)的內(nèi)容編碼類型
    // 發(fā)送到服務(wù)器的數(shù)據(jù)。
    data:JSON.stringify({id : 1, name : "你們都是笨蛋"}),
    async: true , // 默認(rèn)設(shè)置下,所有請(qǐng)求均為異步請(qǐng)求。如果設(shè)置為 false ,則發(fā)送同步請(qǐng)求
    // 請(qǐng)求成功后的回調(diào)函數(shù)。
    success :function(data){
     console.log(data);
     $("#id").html(data.id);
     $("#name").html(data.name);
     $("#author").html(data.author);
    },
    // 請(qǐng)求出錯(cuò)時(shí)調(diào)用的函數(shù)
    error:function(){
     alert("數(shù)據(jù)發(fā)送失敗");
    }
  });
 }
</script>
</head>
<body>
 編號(hào):<span id="id"></span><br>
 書名:<span id="name"></span><br>
 作者:<span id="author"></span><br>
</body>
</html>

所有用到的包如下:

我們有兩個(gè)方法來(lái)進(jìn)行軟件設(shè)計(jì):一個(gè)是讓其足夠的簡(jiǎn)單以至于讓BUG無(wú)法藏身;另一個(gè)就是讓其足夠的復(fù)雜,讓人找不到BUG。前者更難一些。

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

  • MyBatis-Plus框架整合詳細(xì)方法

    MyBatis-Plus框架整合詳細(xì)方法

    MyBatis-Plus是一個(gè) MyBatis 的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開發(fā)、提高效率而生這篇文章主要介紹了MyBatis-Plus框架整合,需要的朋友可以參考下
    2022-04-04
  • Java并發(fā)程序刺客之假共享的原理及復(fù)現(xiàn)

    Java并發(fā)程序刺客之假共享的原理及復(fù)現(xiàn)

    前段時(shí)間在各種社交平臺(tái)“雪糕刺客”這個(gè)詞比較火,而在并發(fā)程序中也有一個(gè)刺客,那就是假共享。本文將通過(guò)示例詳細(xì)講解假共享的原理及復(fù)現(xiàn),需要的可以參考一下
    2022-08-08
  • Mybatis-Plus中的selectByMap使用實(shí)例

    Mybatis-Plus中的selectByMap使用實(shí)例

    Mybatis-Plus來(lái)對(duì)數(shù)據(jù)庫(kù)進(jìn)行增刪改查時(shí),將里面的函數(shù)試了個(gè)遍,接下來(lái)我就將使用selectByMap函數(shù)的簡(jiǎn)單測(cè)試實(shí)例寫出來(lái),方便沒(méi)有使用過(guò)的朋友們快速上手,感興趣的可以了解一下
    2021-11-11
  • java導(dǎo)出數(shù)據(jù)庫(kù)的全部表到excel

    java導(dǎo)出數(shù)據(jù)庫(kù)的全部表到excel

    這篇文章主要為大家詳細(xì)介紹了java導(dǎo)出數(shù)據(jù)庫(kù)的全部表到excel的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-03-03
  • IDEA 自帶的數(shù)據(jù)庫(kù)工具真的很牛(收藏版)

    IDEA 自帶的數(shù)據(jù)庫(kù)工具真的很牛(收藏版)

    這篇文章主要介紹了IDEA 自帶的數(shù)據(jù)庫(kù)工具真的很牛(收藏版),本文以 IntelliJ IDEA/ Mac 版本作為演示,其他版本的應(yīng)該也差距不大,需要的朋友可以參考下
    2021-04-04
  • java 圖片與base64相互轉(zhuǎn)化的示例

    java 圖片與base64相互轉(zhuǎn)化的示例

    這篇文章主要介紹了java 圖片與base64相互轉(zhuǎn)化的示例,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-10-10
  • Java中如何使用?byte?數(shù)組作為?Map?的?key

    Java中如何使用?byte?數(shù)組作為?Map?的?key

    本文將討論在使用HashMap時(shí),當(dāng)byte數(shù)組作為key時(shí)所遇到的問(wèn)題及其解決方案,介紹使用String和List這兩種數(shù)據(jù)結(jié)構(gòu)作為臨時(shí)解決方案的方法,感興趣的朋友跟隨小編一起看看吧
    2023-06-06
  • springboot整合通用Mapper簡(jiǎn)化單表操作詳解

    springboot整合通用Mapper簡(jiǎn)化單表操作詳解

    這篇文章主要介紹了springboot整合通用Mapper簡(jiǎn)化單表操作,通用Mapper是一個(gè)基于Mybatis,將單表的增刪改查通過(guò)通用方法實(shí)現(xiàn),來(lái)減少SQL編寫的開源框架,需要的朋友可以參考下
    2019-06-06
  • 為什么說(shuō)HashMap線程不安全

    為什么說(shuō)HashMap線程不安全

    本文主要介紹了為什么說(shuō)HashMap線程不安全,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • listview點(diǎn)擊無(wú)效的處理方法(推薦)

    listview點(diǎn)擊無(wú)效的處理方法(推薦)

    下面小編就為大家?guī)?lái)一篇listview點(diǎn)擊無(wú)效的處理方法(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05

最新評(píng)論