Spring MVC框架:@ModelAttribute等注解,form表单提交controller传参数【诗书画唱】
前言:本篇的@ModelAttribute注解有时基本上可以说是Spring MVC框架中最重要且最复杂的注解(目前我学习的时候,感觉@ModelAttribute不是很复杂,但是感觉的确挺好用的)。


如果报404的错误,但代码没错,那么建议这样做
内容概括:
边看教程视频边做的学习笔记
controller注解和配置文件中的context标签有很大的关系
@RequestMapping最好还是如图的这种写法,其他的写法容易报错,或者说多个同样的注解,注释了部分,也会可能起作用的,会报错等等
Spring MVC框架中的controller获取输入访问地址,回车后的地址栏传参的值,并且打印其值在控制台的方法1
Spring MVC框架中的controller获取输入访问地址,回车后的地址栏传参的值,并且打印其值在控制台的方法2
跳转到WEB-INF的jsp界面的方法有2种:第1种就是controller中设置个方法,访问这个方法后通过return等来跳转到jsp界面,第2种就是如图所示的方法,用mvc等的标签来配置,path的值为访问路径的尾值,view-name就是执行访问路径后跳转到的jsp界面的值
post方式的form表单提交传参数,controller中也可以获取
使用@PathVariable注解实现界面跳转并且传参的方法和相关的访问路径等的写法,以及如何获取打印其传过来的值的方法1
使用@PathVariable注解实现界面跳转并且传参的方法和相关的访问路径等的写法,以及如何获取打印其传过来的值的方法2
@RequestMapping的作用就是把act账号等参数从url访问路径解析出来,获取其传过来的参数
@ModelAttribute注解的init方法的效果和作用
声明全局变量就可以让多个方法都获得同一个全局变量的值等等
@ModelAttribute注解运用于有返回值的方法
@ModelAttribute注解运用于有返回值的方法 :@RequestMapping的值变成了是要跳转到的jsp界面的名字,return后面的值赋给了@ModelAttribute后面的值,@ModelAttribute后面的值变成了 跳转的jsp界面要用EL表达式接收的变量名
关于form表单提交数据时,要传的参数过多而打包成bean包下的实体类,改传实体类的更简洁明了的方法
代码实例
文件概览
运行效果

边看教程视频边做的学习笔记 START




、





Spring MVC框架中的controller获取输入访问地址,回车后的地址栏传参的值,并且打印其值在控制台的方法2



post方式的form表单提交传参数,controller中也可以获取


使用@PathVariable注解实现界面跳转并且传参的方法和相关的访问路径等的写法,以及如何获取打印其传过来的值的方法2




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

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

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


@ModelAttribute注解运用于有返回值的方法 :@RequestMapping的值变成了是要跳转到的jsp界面的名字,return后面的值赋给了@ModelAttribute后面的值,@ModelAttribute后面的值变成了 跳转的jsp界面要用EL表达式接收的变量名






边看教程视频边做的学习笔记 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
//跳转的页面是/WEB-INF/res.jsp
@RequestMapping("res")
//相当于执行了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类 -->
<!-- 只要是创建在com.SSHC.controller包下面的java类,只要给它添加了@Controller注解
那么这个java类就变成了springmvc中的一个controller -->
<context:component-scan base-package="com.SSHC.controller"/>
<!-- 默认注册RequestMappingHandlerMapping和RequestMappingHandlerAdapter类 -->
<mvc:annotation-driven />
<!-- jsp引用外部js,css等静态资源的解决方法(和上面的标签必须同时出现,否则无法访问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" />
<!-- 优先级设定 -->
<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拦截匹配规则要自已定义,把拦截下来的请求,
依据相应的规则分发到目标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