该项目用来介绍SpringMVC对参数接受的方法:

项目目录树:在前一个项目上修改添加

新添加了Student类和Group类,用来测试整体参数接受

Student.java

package com.orange.model;

public class Student {

    private String name;

    private String password;

    private Group group;

    public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public Group getGroup() {
return group;
} public void setGroup(Group group) {
this.group = group;
} }

Group.java

package com.orange.model;

public class Group {

    private int id;

    private String name;

    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;
} }

提供控制类ParameterController.java

package com.orange.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; import com.orange.model.Student; @Controller
@RequestMapping(value="/parameter")
public class ParameterController { @RequestMapping(value="/tp1") //参数逐个接受
public ModelAndView doParameter1(String name, String password, ModelAndView mav){
mav.addObject("name", name);
mav.addObject("password", password);
mav.setViewName("/parameterShow.jsp");
return mav;
} @RequestMapping(value="/tp2") //参数整体接受,使用Student类中的属性来接受参数
public ModelAndView doParameter2(Student studenttp2, ModelAndView mav){
mav.addObject("studenttp2", studenttp2);
mav.setViewName("/parameterShow.jsp");
return mav;
} @RequestMapping(value="/tp3") //参数域属性接受
public ModelAndView doParameter3(Student studenttp3, ModelAndView mav){
mav.addObject("studenttp3", studenttp3);
mav.setViewName("/parameterShow.jsp");
return mav;
} @RequestMapping(value="/tp4") //参数修正,把提交的参数pname修改为name,参数ppassword修改为password
public ModelAndView doParameter4(@RequestParam("pname") String name, @RequestParam("ppassword") String passwordtp4, ModelAndView mav){
mav.addObject("nametp4", name);
mav.addObject("passwordtp4", passwordtp4);
mav.setViewName("/parameterShow.jsp");
return mav;
} }

测试的jsp文件,

parameter.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!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=GBK">
<base href="<%=basePath %>">
<title>Default Page</title>
</head>
<body>
<div>
<h1>参数逐个接受</h1><br>
<form action="parameter/tp1">
name: <input type="text" name="name"><br>
password: <input type="text" name="password"><br>
<input type="submit" value="submit">
</form>
</div>
<hr>
<div>
<h1>参数整体接受</h1>
<form action="parameter/tp2">
name: <input type="text" name="name"><br>
password: <input type="text" name="password"><br>
<input type="submit" value="submit">
</form>
</div>
<hr>
<div>
<h1>参数域属性接受</h1>
<form action="parameter/tp3">
name: <input type="text" name="name"><br>
password: <input type="text" name="password"><br>
group.id <input type="text" name="group.id"><br>
group.name <input type="text" name="group.name"><br>
<input type="submit" value="submit">
</form>
</div>
<hr>
<div>
<h1>参数修正</h1>
<form action="parameter/tp4">
name: <input type="text" name="pname"><br>
password: <input type="text" name="ppassword"><br>
<input type="submit" value="submit">
</form>
</div>
<hr>
</body>
</html>

展示提交结果的jsp文件parameterShow.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!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=GBK">
<base href="<%=basePath %>">
<title>Default Page</title>
</head>
<body>
<div>
<h1>参数逐个接受</h1>
<c:out value="${name }" /><br>
<c:out value="${password }" /><br>
</div>
<hr>
<div>
<h1>参数整体接受</h1>
<c:out value="${studenttp2.name }" /><br>
<c:out value="${studenttp2.password }" /><br>
</div>
<hr>
<div>
<h1>参数域属性接受</h1>
<c:out value="${studenttp3.name }" /><br>
<c:out value="${studenttp3.password }" /><br>
<c:out value="${studenttp3.group.id }" /><br>
<c:out value="${studenttp3.group.name }" /><br>
</div>
<hr>
<div>
<h1>参数修正</h1>
<c:out value="${nametp4 }" /><br>
<c:out value="${passwordtp4 }" /><br>
</div>
</body>
</html>

通过不同的提交,测试各个接受方式的结果,这里就不在一一展示测试结果了

SpringMVC学习笔记二:参数接受的更多相关文章

  1. springmvc学习笔记二:重定向,拦截器,参数绑定

    Controller方法返回值 返回ModelAndView controller方法中定义ModelAndView对象并返回,对象中可添加model数据.指定view. 返回void 在Contro ...

  2. springMVC学习笔记(二)-----注解和非注解入门小程序

    最近一直在做一个电商的项目,周末加班,忙的都没有时间更新博客了.终于在上周五上线了,可以轻松几天了.闲话不扯淡了,继续谈谈springMvc的学习. 现在,用到SpringMvc的大部分使用全注解配置 ...

  3. SpringMVC学习笔记二:常用注解

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6831976.html  参考:http://www.cnblogs.com/leskang/p/5445698 ...

  4. SpringMVC学习笔记(二)

    一.HandleMapping和HandlerAdapter的讲解 HandleMapping:处理映射器,可以理解为为请求的url查找对应的Controller类. HandlerAdapter:可 ...

  5. springMVC学习笔记二

    六.springmvc的注解 xml的配置→注解 1.新建一个配置文件还是在config下 新建springAnnotation-servlet.xml web.xml 修改初始化为<param ...

  6. SpringMVC 学习笔记(二) @RequestMapping、@PathVariable等注解

    版权声明:本文为博主原创文章,博客地址:http://blog.csdn.net/a67474506?viewmode=contents 1.1. @RequestMapping映射请求 Spring ...

  7. SpringMVC学习笔记二第一个小的程序

    首先:我们要用springmvc来写一个helloworld的例子: 首先我们需要导入所需要的架包: /demo1/WebRoot/WEB-INF/lib/commons-logging-1.1.1. ...

  8. SpringMVC学习笔记之二(SpringMVC高级参数绑定)

    一.高级参数绑定 1.1 绑定数组 需求:在商品列表页面选中多个商品,然后删除. 需求分析:功能要求商品列表页面中的每个商品前有一个checkbok,选中多个商品后点击删除按钮把商品id传递给Cont ...

  9. 史上最全的SpringMVC学习笔记

    SpringMVC学习笔记---- 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于Spring ...

随机推荐

  1. PAT Basic 1034 有理数四则运算(20) [数学问题-分数的四则运算]

    题目 本题要求编写程序,计算2个有理数的和.差.积.商. 输⼊格式: 输⼊在⼀⾏中按照"a1/b1 a2/b2"的格式给出两个分数形式的有理数,其中分⼦和分⺟全是整型范围内的整数, ...

  2. PAT Advanced 1081 Rational Sum (20) [数学问题-分数的四则运算]

    题目 Given N rational numbers in the form "numerator/denominator", you are supposed to calcu ...

  3. PAT Advanced 1067 Sort with Swap(0,*) (25) [贪⼼算法]

    题目 Given any permutation of the numbers {0, 1, 2,-, N-1}, it is easy to sort them in increasing orde ...

  4. PAT Advanced 1050 String Subtraction (20) [Hash散列]

    题目 Given two strings S1 and S2, S = S1 – S2 is defined to be the remaining string afer taking all th ...

  5. MySQL--SHOW TABLE STATUS命令

    show table status 获取表的信息 来自:http://blog.csdn.net/java2000_wl/article/details/7935035

  6. 题解-------CF372C Watching Fireworks is Fun

    传送门 一道有趣的DP 题目大意 城镇中有$n$个位置,有$m$个烟花要放.第$i$个烟花放出的时间记为$t_{i}$,放出的位置记为$a_{i}$.如果烟花放出的时候,你处在位置$x$,那么你将收获 ...

  7. iOS个人中心渐变动画、微信对话框、标签选择器、自定义导航栏、短信验证输入框等源码

    iOS精选源码 简单的个人中心页面-自定义导航栏并予以渐变动画 程序员取悦女票的正确姿势---Tip1(iOS美容篇) iOS 前台重启应用和清除角标的问题 微信原生提醒对话框3.0 JHLikeBu ...

  8. tomcat设置远程监听端口(linux&windows)

    1.Linxu系统: apach/bin/startup.sh开始处中增加如下内容: declare -x CATALINA_OPTS="-server -Xdebug -Xnoagent ...

  9. Java依据集合元素的属性,集合相减

    两种方法:1.集合相减可以使用阿帕奇的一个ListUtils.subtract(list1,list2)方法,这种方法实现必须重写集合中对象的属性的hashCode和equals方法,集合相减判断的会 ...

  10. debian8修改kde桌面语言

    apt-get install kde-l10n-zhcn, language里面改中文 亲测可用 来源:http://tieba.baidu.com/p/2489771177