一: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="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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=UTF-8">
<title>Insert title here</title>
<script type="text/javascript"
src="<%=request.getContextPath()%>/js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(function() {
$("#prev").click(
function() {
$("#form0").attr("action",
"${pageContext.request.contextPath}/user/prev.do");
var currentPage = $("#currentPage").val() - 1;
$("#currentPage").val(currentPage);
alert($("#currentPage").val());
$("#form0").submit();
}) $("#next").click(
function() {
$("#form0").attr("action",
"${pageContext.request.contextPath}/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 com.mi.controller;

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 com.mi.entity.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();
return "redirect:/user/query.do";
} @RequestMapping("/query")
public String queryUserInfo(Model model,HttpServletRequest request) {
pageTableForm = getOperation();
List<User> userList = userInfoServiceImpl.queryUserInfo(pageTableForm.getBeginIndex(), pageTableForm.getEndIndex());
model.addAttribute("userList", userList);
model.addAttribute("pageTableForm", pageTableForm);
return "userInfo";
} public PageTableForm getOperation() {
if (pageTableForm.getCurrentPage() == 0)
pageTableForm.setCurrentPage(1); pageTableForm.setUserCount(getCount());
if (pageTableForm.getUserCount() % 3 == 0) {
pageTableForm.setPageCount(pageTableForm.getUserCount() / pageTableForm.getPageSize());
} else {
pageTableForm.setPageCount(pageTableForm.getUserCount() / pageTableForm.getPageSize() + 1);
} pageTableForm.setBeginIndex(pageTableForm.getCurrentPage() * 3 - 3);
pageTableForm.setEndIndex(pageTableForm.getCurrentPage() * 3); return pageTableForm;
} @RequestMapping("/prev")
public String pagePrev(Model model,HttpServletRequest request,PageTableForm pageTableForm) {
System.out.println(request.getParameter("currentPage"));
System.out.println(pageTableForm.getCurrentPage());
int currentPage = pageTableForm.getCurrentPage()-1;
pageTableForm.setCurrentPage(currentPage);
return "redirect:/user/query.do";
} @RequestMapping("/next")
public String pageNext(Model model) {
int currentPage = pageTableForm.getCurrentPage()+1;
pageTableForm.setCurrentPage(currentPage);
return "redirect:/user/query.do";
} public int getCount() {
return userInfoServiceImpl.getCount();
} 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中方法--request.getParamter()和直接使用对象.get...方法,得出结果如下所示,

我点击上一页,页面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", request.getParameter("name"));
model.addAttribute("password", request.getParameter("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.queryUserInfo(pageTableForm.getBeginIndex(), pageTableForm.getEndIndex());
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", user.getName());
map.put("password", user.getPassword());
return "success";
}

Springmvc controller和jsp页面传值对象类型问题和普通问题的更多相关文章

  1. struts2:JSON在struts中的应用(JSP页面中将对象转换为JSON字符串提交、JSP页面中获取后台Response返回的JSON对象)

    JSON主要创建如下两种数据对象: 由JSON格式字符串创建,转换成JavaScript的Object对象: 由JSON格式字符串创建,转换成JavaScript的List或数组链表对象. 更多关于J ...

  2. 向jsp页面传值时出现乱码

    在一个html页面中用表单向jsp页面传值: 这是html页面 <html> <head> <title>MyBeans.html</title> &l ...

  3. SpringMVC:前台jsp页面和后台传值

    前台jsp页面和后台传值的几种方式: 不用SpringMVC自带的标签 前台---->后台,通过表单传递数据(): 1.jsp页面代码如下,  modelattribute 有没有都行 < ...

  4. 【JavaWEB SSH】jsp页面传值后台Controller 部分值绑定不上实体类

    //前端ajax代码 1 var oldpassword = $('#old_password').val(); var password = $('#L_pass').val(); var user ...

  5. JSP 页面传值方法总结(转)

    原文地址:http://www.cnblogs.com/java-class/p/6358964.html 阅读目录 1. URL 链接后追加参数 2. Form 3. 设置 Cookie 4. 设置 ...

  6. JSP 页面传值方法总结

    JSP 页面间传递参数是项目中经常需要的,这应该算是 web 基本功吧. 试着将各种方式总结下来,需要时可以进行权衡利弊选择最合适的方式. 1. URL 链接后追加参数 <a href=&quo ...

  7. JSP 页面传值

    使用session会话传值并重定向页面 //得到用户提交的值 String name = request.getParameter("username"); String pwd ...

  8. 【页面传值6种方式】- 【JSP 页面传值方法总结:4种】 - 【跨页面传值的几种简单方式3种】

    阅读目录 1. URL 链接后追加参数 2. Form 3. 设置 Cookie 4. 设置 Session JSP 页面间传递参数是项目中经常需要的,这应该算是 web 基本功吧. 试着将各种方式总 ...

  9. JSP页面传值出现中文乱码的问题

    在接收值的jsp页面代码的body里添加: <%request.setCharacterEncoding("utf-8"); %>  //这里是设置utf-8为jsp页 ...

随机推荐

  1. js 闭包之一

    既然说闭包的化,我们就先来说说函数.慢慢的进入进入正题 (1)函数申明 f1(); function f1(){ alert(") }//结果 1 (2)函数定义 f1();var f1=f ...

  2. php经典算法(转载)

    //--------------------  // 基本数据结构算法 //--------------------  //二分查找(数组里查找某个元素)  function bin_sch($arr ...

  3. ZK 页面间参数传递

    1.execution.sendRedirect(url) 当使用方法execution.sendRedirect(url)进行页面跳转时,在url中添加参数:url?test=5: 跳转页面获取参数 ...

  4. JavaScript如何使用this

    学习this的使用必须牢记以下两点 1.this是一个只能在函数内部使用的关键字 2.this指向调用函数的那个对象 下面我们来具体讨论一下this的具体使用方法 第一种:通过函数调用的方式----- ...

  5. 第九周 psp

    团队项目PSP 一:表格     C类型 C内容 S开始时间 E结束时间 I时间间隔 T净时间(mins) 预计花费时间(mins) 讨论 讨论用户界面 9:50 12:45 35 45 80 分析与 ...

  6. 2016总结 wjwdive

    2016 成长:收获最大的,学会了耐心,学会了宽容,学会了不强求.一念放下,万般自在.我真的是晚熟啊 ^_^! . 读书:<小王子>.<了不起的盖茨比>.<和任何人都聊得 ...

  7. [转载] Win PE内安装Windows 7原版镜像 / 安装程序无法创建新的系统分区,也无法定位现有的系统分区

    格式化C盘为NTFS格式 解压ISO安装文件中找到BOOT.BOOTMGR和SOURCES这三个文件到C盘根目录下:或者复制BOOT.BOOTMGR,在C盘新建文件夹SOURCES,复制ISO安装文件 ...

  8. PHP之简单实现MVC框架

    PHP之简单实现MVC框架   1.概述 MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种 ...

  9. cocostudio做出来的界面如何进行分辨率适配,兼论cocos2dx3的多分辨率适配机制,以及retina适配机制

    cocos有很多代码实际上都不再使用了,看代码时反而误导了程序员. 比如一个简单的分辨率适配,我查到了setContentSize,然后调用setContentSize,毫无用处啊!于是乎,我到处查资 ...

  10. 【运维工具】Git代码发布系统

    引言 代码发布系统是互联网公司必备的运维系统,作用主要用户发布业务代码 到 业务服务器 为什么需要代码发布系统 有的同学可能说,我们公司服务器就那么一台,做个发布系统太麻烦了? 不认同这说法 发布系统 ...