紧接着上面的,这儿我们controller返回值用modelandview 上一篇文章点击:上一篇
1.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
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>
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.6.4.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询商品列表</title>
</head>
<body>
<script type="text/javascript">
</script>
<div>
<h2>springmvc测试1</h2>
<!-- <form action="modify.action" method="post"> -->
<form action="get/12.action?add=2" method="post">
<table>
<tr>
<td>id:<input type="text" value="${uservo.id }" name="id"/></td>
</tr>
<tr>
<td>name:<input type="text" value="${uservo.name }" name="name"/></td>
</tr>
<tr>
<td>age:<input type="text" value="${uservo.age }" name="age"/></td>
</tr>
<tr>
<td>address:<input type="text" value="${uservo.address }" name="address"/></td>
</tr>
</table>
<input type="submit" value="提交"/>
</form>
</div>
</body>
</html>
2
.contoller类
package com.bbanp.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.bbanp.pojo.UserVO;
@Controller
public class UserController {
//这个示例返回modelandview,上一个是返回String
@RequestMapping("/get/{id2}")
public ModelAndView getInfo(HttpServletRequest request,HttpServletResponse response,@PathVariable Long id2,
@RequestParam String name,@RequestParam String add){
//输出id2
System.out.println("输出id :"+id2);
//这种pathvariable的不可以通过request获取,得到null,这种是springmvc路径{}的用法,通过pathvariable获取的
System.out.println("再次输出id"+request.getParameter("id2"));
//两个都正常输出,结合上面jsp看这个是input标签中的name对应值
System.out.println("输出name"+name);
System.out.println("再次输出name"+request.getParameter("name"));
//两个都正常输出,这个是form表单路径?问号后面的值传递
System.out.println("输出add"+add);
System.out.println("再次输出add"+request.getParameter("add"));
//返回模型和视图
ModelAndView mav=new ModelAndView();
mav.addObject("name", "塔睿");
mav.setViewName("/index");
return mav;
}
}
/**
* 1.前台给后台传参,不管是通过对象数据绑定 还是 直接接收参数,只要字段是基本数据类型,并且在页面表单中有这个字段的name="**"
* 那么如果不赋值,commit提交时会报错400 语法错误
* 2.所以前台input或者其它标签中有传值到controller的参数,并且后台是基本数据类型接收,那么前台传值时候就不能为空;
* 3.基本数据类型:byte short int long char float double boolean
* 4.引用数据类型:Byte Short Integer Long Character Float Double Boolean
* 记忆:除了Integer和Character,其它除了名字首字母大写,其余相同
* @param request
* @param response
* @param user
* @return
*/