页面回显与URL模板映射
一.页面回显
对于需要返回界面的数据,可以将后台封装好的数据回显至原始jsp界面中。
举个例子:
User.java
package com.zk.data;
public class User {
public String sname;
public String sno;
public String sid;
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
}
UserCustom.java
package com.zk.data; import java.util.HashMap;
import java.util.List;
import java.util.Map; public class UserCustom {
private User user; private List<User> userList; private Map<String,Object> usermap=new HashMap<String,Object>(); public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} public List<User> getUserList() {
return userList;
} public void setUserList(List<User> userList) {
this.userList = userList;
} public Map<String, Object> getUsermap() {
return usermap;
} public void setUsermap(Map<String, Object> usermap) {
this.usermap = usermap;
} @Override
public String toString() {
return "UserCustom [user=" + user + "]";
} }
SpringMVC.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="com.zk.Controller"></context:component-scan> <!-- 配置注解处理器映射器
功能:寻找执行类Controller
-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> <!-- 配置注解处理器适配器
功能:调用controller方法,执行controller
-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean> </beans>
UserController.java
package com.zk.Controller; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry; 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.RequestMethod;
import org.springframework.web.servlet.ModelAndView; import com.zk.data.User;
import com.zk.data.UserCustom; @Controller//<bean id="UserController" class="UserController路径">
public class UserController {
//接受javaBean参数
@RequestMapping("list")
public String list(Model model){
List<User> userList=new ArrayList<User>();
User user1=new User();
user1.setSid("1");
user1.setSname("a");
user1.setSno("No1"); User user2=new User();
user2.setSid("2");
user2.setSname("b");
user2.setSno("No2"); User user3=new User();
user3.setSid("2");
user3.setSname("b");
user3.setSno("No3"); userList.add(user1);
userList.add(user2);
userList.add(user3); model.addAttribute("userList", userList); return "list";
}
}
list.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">
<html>
<head>
<base href="<%=basePath%>"> <title>回显List集合</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<h1>欢迎来SpringMVC</h1>
<table border='1'>
<tr>
<td>姓名</td>
<td>ID</td>
<td>编号</td>
</tr> <c:forEach items="${userList}" var="user">
<tr>
<td>${user.sname}</td>
<td>${user.sid}</td>
<td>${user.sno}</td>
<td><a href="${pageContext.request.contextPath }/${user.sid}.do">修改</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
运行后结果如下:

二.URL模板映射
在上面的例子中,list.jsp页面里有行URL模板映射
<td><a href="${pageContext.request.contextPath }/${user.sid}.do">修改</a></td>
在后台UserController.java的处理逻辑如下:
package com.zk.Controller; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry; 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.RequestMethod;
import org.springframework.web.servlet.ModelAndView; import com.zk.data.User;
import com.zk.data.UserCustom; @Controller//<bean id="UserController" class="UserController路径">
public class UserController {
//接受javaBean参数
@RequestMapping("list")
public String list(Model model){
List<User> userList=new ArrayList<User>();
User user1=new User();
user1.setSid("1");
user1.setSname("a");
user1.setSno("No1"); User user2=new User();
user2.setSid("2");
user2.setSname("b");
user2.setSno("No2"); User user3=new User();
user3.setSid("2");
user3.setSname("b");
user3.setSno("No3"); userList.add(user1);
userList.add(user2);
userList.add(user3); model.addAttribute("userList", userList); return "list";
}
@RequestMapping("/{id}")
public String update(@PathVariable Integer id){
System.out.println(id); return "redirect:list.do";
}
}
它将返回list.jsp界面,并输出id值。

页面回显与URL模板映射的更多相关文章
- Struts2 模型驱动及页面回显
* 要从页面中获取表单元素的值,需要在动作类中声明与页面元素同名的属性.导致动作类中既有javabean又有业务方法. * 将javabean和业务方法进行分离: * 将重 ...
- 基于BootStrap的Collapse折叠(包含回显展开折叠的对应状态)
情况描述:为了改善页面上的input框太多,采用∧∨折叠展开,这个小东西来控制,第一次做,记录一下ヾ(◍°∇°◍)ノ゙下边是Code 代码: //html代码 <div id="col ...
- day78_淘淘商城项目_11_单点登录系统实现 + 用户名回显 + ajax请求跨域问题详解_匠心笔记
课程计划 1.SSO注册功能实现 2.SSO登录功能实现 3.通过token获得用户信息 4.ajax跨域请求解决方案--jsonp 1.服务接口实现 SSO系统就是解决分布式环境下登录问题的,本 ...
- 使用Dropzone上传图片及回显演示样例
一.图片上传所涉及到的问题 1.HTML页面中引入这么一段代码 <div class="row"> <div class="col-md-12" ...
- Spring MVC 基于URL的映射规则(注解版)
好几天没有跟进Spring MVC的学习了,之前看了点源码都忘的差不多了.这次就跟着之前的问题,继续总结下Spring MVC中的小知识. 关于SpringMVC的小demo可以参考这里! url-p ...
- HTML、jsp页面中radio,checkbox,select数据回显功能,默认被选中问题
最近常常遇到各种复选框.单选框.下拉框的默认被选中的问题,开始也是绞尽脑汁的想办法,今天写一篇学习总结的博文来写一下学习总结. 单选框(radio)默认被选中: 一.jstl技术进行回显 <in ...
- Vue -- element-ui el-table 点击tr项页面跳转,返回后缓存回显点击项
页面跳转反显(点击项,点击table滚动的位置,搜索条件,分页回显) 点击table tr项后,页面跳转到下级页面,返回回显搜索条件.当前页码.并将点击项select选中.滚动条也被记录回显跳转时滚动 ...
- JSP页面批量选择&全选操作&选择回显
效果如下: js验证部分: 页面body部分: 附:控制器Controller中验证批量选择条件回显:
- JS实现单选按钮回显时页面效果出现,但选中单选框的值为空
最近做了很多前端页面的工作,遇到的一个感觉很头疼的问题在这里记一下: 经常用JS回显单选框,但是明明从页面效果上来看,单选框已经被选中了,可是却不能触发单选框的change事件,取值的时候用某种方法取 ...
随机推荐
- Gird(2)
目录 grid 布局(2) grid区域属性 网格线名称 grid-template-areas 属性 grid-auto-flow 容器内子元素的属性 grid 布局(2) grid区域属性 网格线 ...
- es5和es6创建新数组的方法
//es5 let array = Array(5) let array = [] //es6 1.let array = Array.of(1,2,3,4,5) 2.let array = Arra ...
- wondiws+centos 双系统
任务:在windows下安装CentOS 1.下载CentOS镜像文件,准备一个未格式化的空间. 2.使用UltraISO将要安装的系统写入U盘. 3.用U盘启动,将系统装入一个空的分区下(未格式化) ...
- 剑指offer-面试题57_1-和为s的两个数字-双指针
/* 题目: 输入一个递增数组和一个s,求和等于s的两个数组中的数字. */ /* 思路: 双指针问题. */ #include<iostream> #include<cstring ...
- python学习随笔2:python判断和循环
1.if-else _username = 'heyue' _password = ' username = input("username:") password = input ...
- Nginx-3.控制nginx
原文 nginx 通过信号来控制.对应linux系统就是用kill命令. The command kill sends the specified signal to the specified pr ...
- Git的学习和使用
1.1. Git 了解git的仓库概念 熟悉何为版本控制,了解分布式版本控制(git)和集中式版本控制(svn) 能够熟练使用git的基本指令完成仓库的初始化/添加/提交/日志/回退/分支等操作 gi ...
- tensorflow张量限幅
本篇内容有clip_by_value.clip_by_norm.gradient clipping 1.tf.clip_by_value a = tf.range(10) print(a) # if ...
- Hadoop学习之路(7)MapReduce自定义排序
本文测试文本: tom 20 8000 nancy 22 8000 ketty 22 9000 stone 19 10000 green 19 11000 white 39 29000 socrate ...
- javascript 问题汇总(1)
1 jquery ajax提交有参数的请求,提示错误“Unsupported Media Type“ 解决:ajax 设置添加 contentType: "application/j ...