最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Spring MVC框架:@ModelAttribute等注解,form表單提交controller傳參數(shù)【詩書畫唱】

2021-03-19 21:22 作者:詩書畫唱  | 我要投稿

前言:本篇的@ModelAttribute注解有時基本上可以說是Spring MVC框架中最重要且最復(fù)雜的注解(目前我學(xué)習(xí)的時候,感覺@ModelAttribute不是很復(fù)雜,但是感覺的確挺好用的)。


如果報404的錯誤,但代碼沒錯,那么建議這樣做



如果報404的錯誤,但代碼沒錯,那么建議這樣做




內(nèi)容概括

邊看教程視頻邊做的學(xué)習(xí)筆記



controller注解和配置文件中的context標(biāo)簽有很大的關(guān)系




@RequestMapping最好還是如圖的這種寫法,其他的寫法容易報錯,或者說多個同樣的注解,注釋了部分,也會可能起作用的,會報錯等等


Spring MVC框架中的controller獲取輸入訪問地址,回車后的地址欄傳參的值,并且打印其值在控制臺的方法1

Spring MVC框架中的controller獲取輸入訪問地址,回車后的地址欄傳參的值,并且打印其值在控制臺的方法2

跳轉(zhuǎn)到WEB-INF的jsp界面的方法有2種:第1種就是controller中設(shè)置個方法,訪問這個方法后通過return等來跳轉(zhuǎn)到j(luò)sp界面,第2種就是如圖所示的方法,用mvc等的標(biāo)簽來配置,path的值為訪問路徑的尾值,view-name就是執(zhí)行訪問路徑后跳轉(zhuǎn)到的jsp界面的值


post方式的form表單提交傳參數(shù),controller中也可以獲取?



使用@PathVariable注解實現(xiàn)界面跳轉(zhuǎn)并且傳參的方法和相關(guān)的訪問路徑等的寫法,以及如何獲取打印其傳過來的值的方法1


使用@PathVariable注解實現(xiàn)界面跳轉(zhuǎn)并且傳參的方法和相關(guān)的訪問路徑等的寫法,以及如何獲取打印其傳過來的值的方法2



@RequestMapping的作用就是把act賬號等參數(shù)從url訪問路徑解析出來,獲取其傳過來的參數(shù)



@ModelAttribute注解的init方法的效果和作用


聲明全局變量就可以讓多個方法都獲得同一個全局變量的值等等




@ModelAttribute注解運用于有返回值的方法





@ModelAttribute注解運用于有返回值的方法 :@RequestMapping的值變成了是要跳轉(zhuǎn)到的jsp界面的名字,return后面的值賦給了@ModelAttribute后面的值,@ModelAttribute后面的值變成了 跳轉(zhuǎn)的jsp界面要用EL表達式接收的變量名




關(guān)于form表單提交數(shù)據(jù)時,要傳的參數(shù)過多而打包成bean包下的實體類,改傳實體類的更簡潔明了的方法


代碼實例

文件概覽?

運行效果

邊看教程視頻邊做的學(xué)習(xí)筆記 START



controller注解和配置文件中的context標(biāo)簽有很大的關(guān)系



、

@RequestMapping最好還是如圖的這種寫法,其他的寫法容易報錯,或者說多個同樣的注解,注釋了部分,也會可能起作用的,會報錯等等


Spring MVC框架中的controller獲取輸入訪問地址,回車后的地址欄傳參的值,并且打印其值在控制臺的方法1(有時更常用)

這種是給獲取的參數(shù)重命名的方法。
Spring MVC框架中的controller獲取輸入訪問地址,回車后的地址欄傳參的值,并且打印其值在控制臺的方法2

跳轉(zhuǎn)到WEB-INF的jsp界面的方法有2種:第1種就是controller中設(shè)置個方法,訪問這個方法后通過return等來跳轉(zhuǎn)到j(luò)sp界面,第2種就是如圖所示的方法,用mvc等的標(biāo)簽來配置,path的值為訪問路徑的尾值,view-name就是執(zhí)行訪問路徑后跳轉(zhuǎn)到的jsp界面的值
post方式的form表單提交傳參數(shù),controller中也可以獲取

post方式的form表單提交傳參數(shù),controller中也可以獲取
使用@PathVariable注解實現(xiàn)界面跳轉(zhuǎn)并且傳參的方法和相關(guān)的訪問路徑等的寫法,以及如何獲取打印其傳過來的值的方法1

使用@PathVariable注解實現(xiàn)界面跳轉(zhuǎn)并且傳參的方法和相關(guān)的訪問路徑等的寫法,以及如何獲取打印其傳過來的值的方法2
@RequestMapping的作用就是把act賬號等參數(shù)從url訪問路徑解析出來,獲取其傳過來的參數(shù)
@ModelAttribute注解的init方法的效果和作用

@ModelAttribute注解的init方法的效果和作用



@ModelAttribute注解的init方法的效果和作用



@ModelAttribute注解運用于有返回值的方法
聲明全局變量就可以讓多個方法都獲得同一個全局變量的值等等


@ModelAttribute注解運用于有返回值的方法 :@RequestMapping的值變成了是要跳轉(zhuǎn)到的jsp界面的名字,return后面的值賦給了@ModelAttribute后面的值,@ModelAttribute后面的值變成了 跳轉(zhuǎn)的jsp界面要用EL表達式接收的變量名

關(guān)于form表單提交數(shù)據(jù)時,要傳的參數(shù)過多而打包成bean包下的實體類,改傳實體類的更簡潔明了的方法





邊看教程視頻邊做的學(xué)習(xí)筆記 END



代碼實例 START

文件概覽 START

package com.SSHC.bean;


public class User {

? ? private String name;

? ? private String Sex;

? ? private String edu;

? ? private String hobbys;

? ? private String birth;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getSex() {

return Sex;

}

public void setSex(String Sex) {

this.Sex = Sex;

}

public String getEdu() {

return edu;

}

public void setEdu(String edu) {

this.edu = edu;

}

public String getHobbys() {

return hobbys;

}

public void setHobbys(String hobbys) {

this.hobbys = hobbys;

}

public String getBirth() {

return birth;

}

public void setBirth(String birth) {

this.birth = birth;

}

}


package com.SSHC.controller;


import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.ModelAttribute;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;


@Controller

public class MaController {

@ModelAttribute

? ? public void init(@RequestParam String act,Model m){

System.out.println(act);

m.addAttribute("_act",act);

? ? }

//http://localhost:8080/Spring6/textBug.jsp

//http://localhost:8080/Spring6/bar?act=Admin

@RequestMapping("bar")

public String bar(){

return "ma";

}

//http://localhost:8080/Spring6/foo?act=Kite

@RequestMapping("foo")

public String foo(){

return "ma";

}

}

package com.SSHC.controller;


import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.ModelAttribute;

import org.springframework.web.bind.annotation.RequestMapping;


@Controller

public class MrController {

//http://localhost:8080/Spring6/res

//跳轉(zhuǎn)的頁面是/WEB-INF/res.jsp

@RequestMapping("res")

//相當(dāng)于執(zhí)行了request.setAttribute("msg",birth);

@ModelAttribute("msg")

? ? public String hw(){

String birth = "2000-6-6";

? ? return birth;

? ? }

}


package com.SSHC.controller;


import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.ModelAttribute;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;


import com.SSHC.bean.User;


@Controller

public class PubController {

//http://localhost:8080/Spring6/lg?act=admin&pwd=123&flag=true

@RequestMapping("lg")

? ? public String doLogin(@RequestParam String act

? ? ,@RequestParam String pwd,@RequestParam Boolean flag){

//獲取賬號和密碼

System.out.println(act);

System.out.println(pwd);

System.out.println(flag);

? ? return "success";

? ? }

//http://localhost:8080/Spring6/toLg

// @RequestMapping("toLg")

// public String toLogin(){

// return "login";

// }

//http://localhost:8080/Spring6/99/topics/abc

@RequestMapping(value = "{num}/topics/{txt}")

public String reg(@PathVariable("num") int n,

@PathVariable("txt")String t){

System.out.println(n);

System.out.println(t);

return "success";

}

@RequestMapping(value = "doReg")

public String doReg(@ModelAttribute("_user") User u){

//request.setAttribute("_user",u);

return "show";

}

}





package com.SSHC.controller;


import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;


@Controller

@RequestMapping("test")

public class Test {

//http://localhost:8080/Spring6/test/ts

@RequestMapping("ts")

? ? public String perform(){

? ? System.out.println("Hello world");

? ? return "index";

? ? }

}

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%

? ? String path = request.getContextPath();

? ? String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

? ? <head>

? ? ? ? <base hreff="<%=basePath%>">

? ? ? ? <title></title>

? ? ? ? <meta http-equiv="pragma" content="no-cache">

? ? ? ? <meta http-equiv="cache-control" content="no-cache">

? ? ? ? <meta http-equiv="expires" content="0">

? ? ? ? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

? ? ? ? <meta http-equiv="description" content="This is my page">

? ? ? ? <style type="text/css">

? ? ? ? ? ? *{

? ? ? ? ? ? ? ? font-size: 50px;

? ? ? ? ? ? }

? ? ? ? </style>

? ? </head>

? ? <body>

? ? ? ? <form action="lg?flag=true" method="post">

? ? ? ? ? ? <input type="text" placeholder="請輸入賬號" name="act" />

? ? ? ? ? ? <br>

? ? ? ? ? ? <input type="password" placeholder="請輸入密碼" name="pwd" />

? ? ? ? ? ? <br>

? ? ? ? ? ? <input type="submit" value="提交" />

? ? ? ? </form>

? ? </body>

</html>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%

? ? String path = request.getContextPath();

? ? String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

? ? <head>

? ? ? ? <base hreff="<%=basePath%>">

? ? ? ? <title></title>

? ? ? ? <meta http-equiv="pragma" content="no-cache">

? ? ? ? <meta http-equiv="cache-control" content="no-cache">

? ? ? ? <meta http-equiv="expires" content="0">

? ? ? ? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

? ? ? ? <meta http-equiv="description" content="This is my page">

? ? </head>

? ? <body>

? ? ? ? <h1>${_act }測試成功</h1>

? ? </body>

</html>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%

? ? String path = request.getContextPath();

? ? String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

? ? <head>

? ? ? ? <base hreff="<%=basePath%>">

? ? ? ? <title></title>

? ? ? ? <meta http-equiv="pragma" content="no-cache">

? ? ? ? <meta http-equiv="cache-control" content="no-cache">

? ? ? ? <meta http-equiv="expires" content="0">

? ? ? ? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

? ? ? ? <meta http-equiv="description" content="This is my page">

? ? ? ? <style type="text/css">

? ? ? ? ? ? *{

? ? ? ? ? ? ? ? font-size: 50px;

? ? ? ? ? ? }

? ? ? ? </style>

? ? </head>

? ? <body>

? ? ? ? <form action="doReg" method="post">

? ? ? ? ? ? <input type="text" name="name" placeholder="請輸入姓名"/>

? ? ? ? ? ? <br>

? ? ? ? ? ? <input type="radio" name="Sex" value="男"/>男

? ? ? ? ? ? <input type="radio" name="Sex" value="女"/>女

? ? ? ? ? ? <br>

? ? ? ? ? ? <select name="edu">

? ? ? ? ? ? ? ? <option value="">請選擇</option>

? ? ? ? ? ? ? ? <option value="本科">本科</option>

? ? ? ? ? ? ? ? <option value="大專">大專</option>

? ? ? ? ? ? ? ? <option value="高中">高中</option>

? ? ? ? ? ? ? ? <option value="初中">初中</option>

? ? ? ? ? ? </select>

? ? ? ? ? ? <br>

? ? ? ? ? ? <input type="checkbox" name="hobbys" value="唱歌"/>唱歌

? ? ? ? ? ? <input type="checkbox" name="hobbys" value="街舞"/>街舞

? ? ? ? ? ? <input type="checkbox" name="hobbys" value="滑冰"/>滑冰

? ? ? ? ? ? <input type="checkbox" name="hobbys" value="吉他"/>吉他

? ? ? ? ? ? <br>

? ? ? ? ? ? <input type="text" name="birth" placeholder="請輸入生日" />

? ? ? ? ? ? <br>

? ? ? ? ? ? <input type="submit" value="提交" />

? ? ? ? </form>

? ? </body>

</html>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%

? ? String path = request.getContextPath();

? ? String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

? ? <head>

? ? ? ? <base hreff="<%=basePath%>">

? ? ? ? <title></title>

? ? ? ? <meta http-equiv="pragma" content="no-cache">

? ? ? ? <meta http-equiv="cache-control" content="no-cache">

? ? ? ? <meta http-equiv="expires" content="0">

? ? ? ? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

? ? ? ? <meta http-equiv="description" content="This is my page">

? ? </head>

? ? <body>

? ? ? ? <h1>${msg }</h1>

? ? </body>

</html>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%

? ? String path = request.getContextPath();

? ? String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

? ? <head>

? ? ? ? <base hreff="<%=basePath%>">

? ? ? ? <title></title>

? ? ? ? <meta http-equiv="pragma" content="no-cache">

? ? ? ? <meta http-equiv="cache-control" content="no-cache">

? ? ? ? <meta http-equiv="expires" content="0">

? ? ? ? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

? ? ? ? <meta http-equiv="description" content="This is my page">

? ? </head>

? ? <body>

? ? ? ? <h1>${_user.name }</h1>

? ? ? ? <h1>${_user.Sex }</h1>

? ? ? ? <h1>${_user.edu }</h1>

? ? ? ? <h1>${_user.hobbys }</h1>

? ? ? ? <h1>${_user.birth }</h1>

? ? </body>

</html>

<?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:context="http://www.springframework.org/schema/context"

? ? xmlns:mvc="http://www.springframework.org/schema/mvc"

? ? xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

? ? ? ? http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd

? ? ? ? http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">? ? ? ? ? ? ? ? ? ? ? ?

? ? <!-- 掃描指定的包和它的子包中的所有java類 -->

? ? <!-- 只要是創(chuàng)建在com.SSHC.controller包下面的java類,只要給它添加了@Controller注解

? ? ? ? ? ? ? ? ? 那么這個java類就變成了springmvc中的一個controller -->

? ? <context:component-scan base-package="com.SSHC.controller"/>

? ? <!-- 默認注冊RequestMappingHandlerMapping和RequestMappingHandlerAdapter類 -->

? ? <mvc:annotation-driven />

? ? <!-- jsp引用外部js,css等靜態(tài)資源的解決方法(和上面的標(biāo)簽必須同時出現(xiàn),否則無法訪問url) -->

? ? <mvc:default-servlet-handler />

? ? <!-- 配置視圖名稱解析器 -->

? ? <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"?

? ? ? ? ? ? id="internalResourceViewResolver">

? ? ? ? <!-- 前綴 -->

? ? ? ? <!-- 將所有的jsp文件存放在/WEB-INF/my/目錄下 -->

? ? ? ? <property name="prefix" value="/WEB-INF/" />

? ? ? ? <!-- 后綴 -->

? ? ? ? <property name="suffix" value=".jsp" />

? ? ? ? <!-- 優(yōu)先級設(shè)定 -->

? ? ? ? <property name="order" value="10"></property>

? ? </bean>

? ? <!-- http://localhost:8080/Spring6/toLg -->

? ? <mvc:view-controller path="/toLg" view-name="login" />

? ? <!-- http://localhost:8080/Spring6/toReg -->

? ? <mvc:view-controller path="/toReg" view-name="reg" />

</beans>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%

? ? String path = request.getContextPath();

? ? String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

? ? <head>

? ? ? ? <base hreff="<%=basePath%>">

? ? ? ? <title></title>

? ? ? ? <meta http-equiv="pragma" content="no-cache">

? ? ? ? <meta http-equiv="cache-control" content="no-cache">

? ? ? ? <meta http-equiv="expires" content="0">

? ? ? ? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

? ? ? ? <meta http-equiv="description" content="This is my page">

? ? </head>

? ? <body>

? ? ? ? <h1>注冊成功</h1>

? ? </body>

</html>

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

? <display-name>Spring6</display-name>

? <!-- controller中文亂碼處理,注意一點:要配置在所有過濾器的前面 -->

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

? </filter>

? <filter-mapping>

? ? <filter-name>CharacterEncodingFilter</filter-name>

? ? <url-pattern>/*</url-pattern>

? </filter-mapping>

? <!-- springmvc框架配置 -->

? <!-- DispatcherServlet是前置控制器,配置在web.xml文件中的。

? ? ? ? ? ? 攔截匹配的請求,Servlet攔截匹配規(guī)則要自已定義,把攔截下來的請求,

? ? ? ? ? ? 依據(jù)相應(yīng)的規(guī)則分發(fā)到目標(biāo)Controller來處理,

? ? ? ? ? ? 是配置spring MVC的第一步。 -->

? <servlet>

? ? ? <servlet-name>springmvc</servlet-name>

? ? ? <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

? ? ? <init-param>

? ? ? ? ? <!-- 覆蓋默認配置文件{servlet-name}-servlet.xml -->

? ? ? ? ? <param-name>contextConfigLocation</param-name>

? ? ? ? ? <param-value>/WEB-INF/springmvc-servlet.xml</param-value>

? ? ? </init-param>

? ? ? <load-on-startup>1</load-on-startup>

? </servlet>

? <servlet-mapping>

? ? ? <servlet-name>springmvc</servlet-name>

? ? ? <url-pattern>/</url-pattern>

? </servlet-mapping>

? <welcome-file-list>

? ? <welcome-file>index.html</welcome-file>

? ? <welcome-file>index.htm</welcome-file>

? ? <welcome-file>index.jsp</welcome-file>

? ? <welcome-file>default.html</welcome-file>

? ? <welcome-file>default.htm</welcome-file>

? ? <welcome-file>default.jsp</welcome-file>

? </welcome-file-list>

</web-app>



運行效果:



文件概覽 END



代碼實例?END


Spring MVC框架:@ModelAttribute等注解,form表單提交controller傳參數(shù)【詩書畫唱】的評論 (共 條)

分享到微博請遵守國家法律
潼南县| 杂多县| 兴山县| 海安县| 宿迁市| 海盐县| 扎赉特旗| 陵川县| 霞浦县| 沁源县| 庐江县| 仙游县| 松溪县| 长宁区| 阿鲁科尔沁旗| 瑞安市| 舒城县| 将乐县| 太仓市| 镇坪县| 分宜县| 大同市| 永清县| 凉山| 红河县| 萨嘎县| 府谷县| 旬阳县| 长治县| 巩义市| 合川市| 图木舒克市| 阳春市| 商河县| 罗江县| 成武县| 武穴市| 景谷| 达州市| 东乡族自治县| 伊金霍洛旗|