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

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

服務器之家 - 編程語言 - Java教程 - 詳解SpringMVC注解版前臺向后臺傳值的兩種方式

詳解SpringMVC注解版前臺向后臺傳值的兩種方式

2020-09-10 13:56gwblue Java教程

本篇文章主要介紹了詳解SpringMVC注解版前臺向后臺傳值的兩種方式,具有一定的參考價值,有興趣的可以了解一下。

一、概述。

在很多企業的開法中常常用到springmvc+spring+hibernate(mybatis)這樣的架構,springmvc相當于struts是頁面到contorller直接的交互的框架也是界面把信息傳輸到contorller層的一種架構,通過這個架構可以讓我們把頁面和contorller層解耦,使得開發人員的分工更加明確。

二、代碼演示。

1、首先配置springmvc環境。

1.1導入jar。

詳解SpringMVC注解版前臺向后臺傳值的兩種方式

值得注意的是紅色標記的commons-logging這個jar包一定得引入進去不然會報錯。

1.2、xml配置文件。

web.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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">
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.spring</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

springmvc-servlet.xml

?
1
2
3
4
5
6
7
8
9
10
11
<?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">
  <context:component-scan base-package="com.gaowei.controller" />
</beans>

2、前臺界面代碼。

login.jsp

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

no.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
<%@ 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>
no!
</body>
</html>

ok.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
<%@ 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>
 ok! welcome:${username}
</body>
</html>

3、contorller層接收前臺的兩種方式。

方式一:

利用@requestparam這個注解

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.gaowei.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
public class login {
 
  //方式一
  @requestmapping("/login")
  public string login(@requestparam("username") string username,
            @requestparam("password") string password,model model){
    if (username.equals(password)) 
    {
      model.addattribute("username", username);
      return "ok.jsp";
    } else {
      return "no.jsp";
    }
  }
}

方式二:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.gaowei.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
public class login {
@requestmapping("/login")
  public string login(string username,string password,model model){
    if (username.equals(password)) 
    {
      model.addattribute("username", username);
      return "ok.jsp";
    } else {
      return "no.jsp";
    }
  }
 
}

4、界面結果。

第一種傳值方式:

詳解SpringMVC注解版前臺向后臺傳值的兩種方式詳解SpringMVC注解版前臺向后臺傳值的兩種方式

第二種傳值方式:

詳解SpringMVC注解版前臺向后臺傳值的兩種方式

三、總結。

這里體現出了springmvc傳值方式的多樣性滿足了開發人員的不同需求。第一種用來表單的提交。第二種用來界面間相互傳值,也為了方便開發人員利用ajax。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/gwblue/article/details/42966017

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美成年私人网站 | 日日鲁夜夜视频热线播放 | 99精品视频在线观看免费 | 久久久成人免费视频 | 久久亚洲第一 | 成人午夜a| 国产精品夜色视频一级区 | 国产91一区二区三区 | 色婷婷久久久 | xxxx69hd一hd72 | 高潮激情aaaaa免费看 | 99精品视频在线观看免费播放 | 久久2019中文字幕 | 久久狠狠高潮亚洲精品 | 免费黄色欧美视频 | 欧美性受xxx黑人xyx性爽 | 毛片免费在线观看视频 | 精品一区二区三区欧美 | 久久免费视频一区二区三区 | av免费在线不卡 | 久久久免费观看完整版 | 毛片在线免费观看完整版 | 亚洲精品aa | 黄视频免费观看 | 久久久久9999 | 视频一区二区三区在线播放 | 成年人黄色免费电影 | 牛牛视频在线 | 91成人免费看片 | 三片在线观看 | 久久亚洲精选 | 国产视频导航 | 午夜精品小视频 | 大学生一级毛片在线视频 | 国产精品视频 | xnxx 日本免费 | 999久久久免费视频 久久精品国产精品亚洲 | 欧美一级视频免费看 | 天天草天天干天天射 | 国产午夜亚洲精品 | lutube成人福利在线观看污 |