Springmvccontroller和jsp页⾯传值对象类型问题和普通问题⼀:JSP-->controller
1.当jsp页⾯传递的值是对象类型时候⽐如User.name User.age的user对象传递,需要以下操作
jsp页⾯提供对应标签的value必须存在且合法,name属性只能是对象的具体属性名,不需要写成对象.属性名的形式,例如:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="java.sun/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript"
src="<%=ContextPath()%>/js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(function() {
$("#prev").click(
function() {
$("#form0").attr("action",
"${tPath}/user/prev.do");
var currentPage = $("#currentPage").val() - 1;
$("#currentPage").val(currentPage);
alert($("#currentPage").val());
$("#form0").submit();
})
$("#next").click(
function() {
$("#form0").attr("action",
"${tPath}/user/next.do");
$("#form0").submit();
})
})
</script>
</head>
<body>
<form id="form0" method="POST">
<table border="1">
<thead>
<tr>
<td width="60px">id</td>
<td width="120px">name</td>
<td width="60px">age</td>
</tr>
</thead>
<tbody>
<c:forEach var="user" items="${userList}" varStatus="status">
<tr>
<td>${user.id}</td>
<td>${user.userName}</td>
<td>${user.age}</td>
</tr>
</c:forEach>
<tr>
<td><c:if test="${pageTableForm.currentPage > 1}">
<input id="prev" type="button" value="上⼀页">
</c:if></td>
<td>当前<label>${pageTableForm.currentPage}</label>页/共<label>${pageTableForm.pageCount}</label>页</td>
<td><c:if test="${pageTableForm.currentPage < pageTableForm.pageCount}">
<input id="next" type="button" value="下⼀页">
</c:if></td>
</tr>
</tbody>
</table>
<input id="currentPage" type="text" name="currentPage" value="${pageTableForm.currentPage}">
</form>
</body>
</html>
这⾥的
<input id="currentPage" type="text" name="currentPage" value="${pageTableForm.currentPage}">
我⾃⼰定义的⼀个封装属性的javabean对象PageTableForm,⽤来存放表单内容,个⼈习惯别较真⼉
package com.mi.form;
public class PageTableForm {
private int currentPage;// 当前页
private int pageSize = 3;// 每页记录数
private int beginIndex;// 开始位置
private int endIndex;// 结束位置
private int pageCount;// 共多少页
private int userCount;// 共多少条记录
...
省略get set
}
此时的value="${pageTableForm.currentPage}"的值为⼀个int类型且存在的数字,提交表单后到对应的requestmapping的⽅法中,代码如下package ller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import ity.User;
import com.mi.form.PageTableForm;
import com.mi.service.impl.UserInfoServiceImpl;
@Controller
@RequestMapping("/user")
public class UserInfoCotroller {
@Autowired
private UserInfoServiceImpl userInfoServiceImpl;
private PageTableForm pageTableForm;
@RequestMapping("/init")
public String init(){
pageTableForm = new PageTableForm();
springmvc常用标签
return "redirect:/user/query.do";
}
@RequestMapping("/query")
public String queryUserInfo(Model model,HttpServletRequest request) {
pageTableForm = getOperation();
List<User> userList = userInfoServiceImpl.BeginIndex(), EndIndex());
model.addAttribute("userList", userList);
model.addAttribute("pageTableForm", pageTableForm);
return "userInfo";
}
public PageTableForm getOperation() {
if (CurrentPage() == 0)
pageTableForm.setCurrentPage(1);
pageTableForm.setUserCount(getCount());
if (UserCount() % 3 == 0) {
pageTableForm.UserCount() / PageSize());
} else {
pageTableForm.UserCount() / PageSize() + 1);
}
pageTableForm.CurrentPage() * 3 - 3);
pageTableForm.CurrentPage() * 3);
return pageTableForm;
}
@RequestMapping("/prev")
public String pagePrev(Model model,HttpServletRequest request,PageTableForm pageTableForm) {
System.out.Parameter("currentPage"));
System.out.CurrentPage());
int currentPage = CurrentPage()-1;
pageTableForm.setCurrentPage(currentPage);
return "redirect:/user/query.do";
}
@RequestMapping("/next")
public String pageNext(Model model) {
int currentPage = CurrentPage()+1;
pageTableForm.setCurrentPage(currentPage);
return "redirect:/user/query.do";
}
public int getCount() {
Count();
}
public UserInfoServiceImpl getUserInfoServiceImpl() {
return userInfoServiceImpl;
}
public void setUserInfoServiceImpl(UserInfoServiceImpl userInfoServiceImpl) {
this.userInfoServiceImpl = userInfoServiceImpl;
}
public PageTableForm getPageTableForm() {
return pageTableForm;
}
public void setPageTableForm(PageTableForm pageTableForm) {
this.pageTableForm = pageTableForm;
}
}
红⾊部分就是提交的⽅法,可以看出在⽅法参数中要声明⼀下对象,这⾥我为了是实验⽐较数据传到
后台是否正确,所以使⽤了2中⽅法--Paramter()和直接使⽤对象.⽅法,得出结果如下所⽰,
我点击上⼀页,页⾯alert⼀个跳转页⾯的页码,现在是从第⼆页翻到第⼀页,所以是1,这样我们后台2次正确的情况应该也都是1(⽇语操作系统,别在意这些,不是乱码)
多点了⼀次,别在意这些,重点是后台获取的数据就是前台我们想要的
2.当jsp->后台是普通数据的时候,实际项⽬中这种情况不多,但是还是总结⼀下
暂时挂起,关于这些我想等到实际⽤到时再列,上1中已经列了⼀种⽅法,另外就是利⽤注解去值
页⾯如下
<form action="login2" method="post">
⽤户:<input type="text" name="name"><br><br>
密码:<input type="text" name="password"><br><br>
<input type="submit" value="确定">
</form>
普通数据(⾮对象)的数据有2种⽅法传递到后台,request获取+注解版的request
1):request获取
/**
* 使⽤HttpServletRequest获取
*/
@RequestMapping("/login1")
public String login1(HttpServletRequest request,Model model){
model.addAttribute("name", Parameter("name"));
model.addAttribute("password", Parameter("password"));
return "success";
}
2):注解版的request
/**
* spring⾃动将表单参数注⼊到⽅法参数,参数值和页⾯name属性⼀致时可以省去@RequestParam注解
*/
@RequestMapping("/login2")
public String login2(@RequestParam("name") String name, String password,Model model){
model.addAttribute("name", name);
model.addAttribute("password", password);
return "success";
}
实体类User不再列出了
⼆:controller-->JSP
有2中办法(实际更多,不过存在重复嫌疑列举常⽤的就好),⽤model+⽤map
1):利⽤model对象添加数据到属性中,页⾯可以使⽤EL表达式获取
@RequestMapping("/init")
public String init(){
pageTableForm = new PageTableForm();
return "redirect:/user/query.do";
}
@RequestMapping("/query")
public String queryUserInfo(Model model,HttpServletRequest request) {
pageTableForm = getOperation();
List<User> userList = userInfoServiceImpl.BeginIndex(), EndIndex());        model.addAttribute("userList", userList);
model.addAttribute("pageTableForm", pageTableForm);
return "userInfo";
}
JSP:
<form id="form0" method="POST">
<table border="1">
<thead>
<tr>
<td width="60px">id</td>
<td width="120px">name</td>
<td width="60px">age</td>
</tr>
</thead>
<tbody>
<c:forEach var="user" items="${userList}" varStatus="status">
<tr>
<td>${user.id}</td>
<td>${user.userName}</td>
<td>${user.age}</td>
</tr>
</c:forEach>
<tr>
<td><c:if test="${pageTableForm.currentPage > 1}">
<input id="prev" type="button" value="上⼀页">
</c:if></td>
<td>当前<label>${pageTableForm.currentPage}</label>页/共<label>${pageTableForm.pageCount}</label>页</td>
<td><c:if test="${pageTableForm.currentPage < pageTableForm.pageCount}">
<input id="next" type="button" value="下⼀页">
</c:if></td>
</tr>
</tbody>
</table>
<input id="currentPage" type="text" name="currentPage" value="${pageTableForm.currentPage}">
</form>
2):使⽤map⽅式设值,JSP同上不再列出
@RequestMapping("/login4")
public String login4(User user, Map<String, Object> map){
map.put("name", Name());
map.put("password", Password());
return "success";
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。