一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

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

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注解版前臺向后臺傳值的兩種方式,具有一定的參考價值,有興趣的可以了解一下。

一、概述。

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

二、代碼演示。

1、首先配置springmvc環(huán)境。

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注解版前臺向后臺傳值的兩種方式

三、總結。

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

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲精品国产一区二区第一页 | 爱操综合 | 国产高清好大好夹受不了了 | 亚洲福利一区二区精品秒拍 | 99精品久久99久久久久久 | 车上小婕子系列辣文小说 | 国产麻豆91欧美一区二区 | 日韩成a人片在线观看日本 日韩不卡一区二区 | 亚洲国产精品网站久久 | 操姓| 国模孕妇季玥337p人体 | 日本在线不卡免 | 国产精品久久久久久久久久久搜索 | 91精品手机国产在线观 | 久久九九精品国产自在现线拍 | 国产精品久久久久久久久久久搜索 | 高清国产欧美一v精品 | 色综合天天综合网看在线影院 | 国产黄频在线观看 | 免费观看视频在线 | 香蕉免费一区二区三区在线观看 | 农村妇女野外牲交一级毛片 | 边摸边吃奶又黄激烈视频韩国 | 四缺一的小说 | 欧美日韩国产一区二区三区不卡 | 性做久久久久久久久老女人 | 日本人黄色 | 亚洲日日操 | 香蕉eeww99国产精选播放 | 日韩永久在线观看免费视频 | 学校捏奶揉下面污文h | 青草青草伊人精品视频 | 国产精品久久久久无毒 | 日本国产一区二区三区 | 亚洲欧美久久婷婷爱综合一区天堂 | 欧美日韩一区二区三在线 | 激情五月姐姐 | 91精品国产综合久久精品 | 国产精品视频第一页 | 久久久精品3d动漫一区二区三区 | 深夜影院深a|