springmvc复习笔记----springmvc姓名年龄例子:RequestParam 试水
继续
继上节http://www.cnblogs.com/tk55/p/6652394.html
重要部分颜色突出
结构
包
web.xml
乱码处理方面设置 <url-pattern>*</url-pattern>对所有对象起作用
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>springmvc01</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <filter>
<filter-name>characterEncodingfilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingfilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
</web-app>
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 自动扫描加载注解的包 -->
<context:component-scan base-package="com.ij34.bean"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp" ></property>
</bean>
</beans>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% response.sendRedirect("students/list"); %>
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" import="java.util.*"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>
</head>
<body>
<a href="${pageContext.request.contextPath}/students/preSave">添加学生</a>
<table>
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>操作</th>
</tr>
<c:forEach var="student" items="${studentlist}">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
<td><a href="${pageContext.request.contextPath }/students/preSave?id=${student.id}">修改</a>
<a href="${pageContext.request.contextPath }/students/delete?id=${student.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
</head>
<body>
<form action="${pageContext.request.contextPath }/students/save"method="post">
<table>
<tr>
<th colspan="2">学生添加</th>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age"/></td>
</tr>
<tr>
<td colspan="2"><input value="提交" type="submit"/></td>
</tr>
</table>
</form>
</body>
</html>
update.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
</head>
<body>
<form action="${pageContext.request.contextPath }/students/save"method="post">
<table>
<tr>
<th colspan="2">学生修改</th>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="name" value="${students.name }"/></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age" value="${students.age }"/></td>
</tr>
<tr>
<td><input type="hidden" name="id" value="${students.id }"/> </td>
<td colspan="2"><input value="提交" type="submit"/></td>
</tr>
</table>
</form>
</body>
</html>
Students.java
package com.ij34.model; public class Students {
private int id;
private String name;
private int age;
public Students() {
// TODO Auto-generated constructor stub
}
public Students(int id,String name,int age) {
this.id=id;
this.name=name;
this.age=age; }
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;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} }
Testhello.java
package com.ij34.bean; import java.util.ArrayList;
import java.util.List; 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.ij34.model.Students; @Controller
@RequestMapping("/students") //外部请求的 包括update.jsp里${students.name }
public class Testhello {
private static List<Students> studentlist=new ArrayList<Students>();
static{
studentlist.add(new Students(1, "林肯", 39));
studentlist.add(new Students(2, "奥巴马", 42));
studentlist.add(new Students(3, "川普", 66));
studentlist.add(new Students(4, "布什", 56));
}
@RequestMapping("/list") //@RequestMapping 请求映射
public ModelAndView list() {
ModelAndView mav=new ModelAndView(); //ModelAndView 返回模型和视图
mav.addObject("studentlist", studentlist);
mav.setViewName("students/list");
return mav;
}
@RequestMapping("/preSave")
public ModelAndView preSave(@RequestParam(value="id",required=false)String id ){//@RequestParam 请求参数
ModelAndView mav=new ModelAndView();
if(id!=null){
mav.addObject("students", studentlist.get(Integer.parseInt(id)-1));
mav.setViewName("students/update");
}else {
mav.setViewName("students/add");
}
return mav; } @RequestMapping("/save")
public String save(Students student){
if(student.getId()!=0){
Students s=studentlist.get(student.getId()-1);
s.setName(student.getName());
s.setAge(student.getAge());
}else{
studentlist.add(student);
}
return "redirect:/students/list";
}
@RequestMapping("/delete")
public String delete(@RequestParam("id")int id){//@RequestParam 请求参数
studentlist.remove(id-1);
return "redirect:/students/list"; //重定向redirect或者forward转发
}
}
springmvc复习笔记----springmvc姓名年龄例子:RequestParam 试水的更多相关文章
- springmvc复习笔记----springmvc最简单的第一个例子:RequestMapping试水
结构 用到的包 web.xml <url-pattern>/</url-pattern>中可以换成其他的后缀*.do ,*. sb …… <?xml version=& ...
- springmvc复习笔记----文件上传multipartResolver
结构 web.xml <?xml version="1.0" encoding=&q ...
- springmvc复习笔记----Restful 风格,PathVariable获取 Url实例
结构 包与之前相同 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&qu ...
- 【转载】SpringMVC学习笔记
转载于:SpringMVC笔记 SpringMVC 1.SpringMVC概述 MVC: Model(模型): 数据模型,提供要展示的数据,:Value Object(数据Dao) 和 服务层(行为S ...
- SpringMVC学习笔记之二(SpringMVC高级参数绑定)
一.高级参数绑定 1.1 绑定数组 需求:在商品列表页面选中多个商品,然后删除. 需求分析:功能要求商品列表页面中的每个商品前有一个checkbok,选中多个商品后点击删除按钮把商品id传递给Cont ...
- springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定
springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定 标签: springmvc springmvc学习笔记13-springmvc注解开发之集合类型參数绑定 数组绑定 需 ...
- 史上最全的SpringMVC学习笔记
SpringMVC学习笔记---- 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于Spring ...
- springmvc学习笔记--REST API的异常处理
前言: 最近使用springmvc写了不少rest api, 觉得真是一个好框架. 之前描述的几篇关于rest api的文章, 其实还是不够完善. 比如当遇到参数缺失, 类型不匹配的情况时, 直接抛出 ...
- springmvc学习笔记---面向移动端支持REST API
前言: springmvc对注解的支持非常灵活和飘逸, 也得web编程少了以往很大一坨配置项. 另一方面移动互联网的到来, 使得REST API变得流行, 甚至成为主流. 因此我们来关注下spring ...
随机推荐
- 剑指offer-学习笔记
前言:18/06/06开始学习,每个程序都会用C写一遍,因书中用C++举例,也会换种思路写,供学习和参考!!!很推荐这本书很不错,准备入手,一般不买实体书,都用电子书,因一般都看一遍,但这本会看很多遍 ...
- web进修之—Hibernate HQL(7)
概述 HQL是Hibernate封装成为面向对象的数据库查询语言,具有如下特点: 面向对象,包括继承.多态和关联之类的概念,SQL操作的数据库的表,HQL更像是操作对象 大小写敏感,只对对象和属性敏感 ...
- python练习七—P2P下载
最近有些事儿比较忙,python的学习就断断续续,这个练习来得比预期的晚,不过还好,不管做什么,我都希望能认真对待,认真做好每一件事. 引入 这个练习原书中称作“使用XML-RPC进行文件共享”,题目 ...
- MongoDB副本集(一主两从)读写分离、故障转移功能环境部署记录
Mongodb是一种非关系数据库(NoSQL),非关系型数据库的产生就是为了解决大数据量.高扩展性.高性能.灵活数据模型.高可用性.MongoDB官方已经不建议使用主从模式了,替代方案是采用副本集的模 ...
- HTML语法介绍
一 基本标签(块级标签和内联标签) <hn>: n的取值范围是1~6; 从大到小. 用来表示标题. <p>: 段落标签. 包裹的内容被换行.并且也上下内容之间有一行空白. &l ...
- win8 notepad++ 设置无法保存
前些天买了新笔记本.装的是win8 ,后来装了notepad++ ,最新版的,改了字体,下次从新打开之后,发现字体改动无效.后来试了一下“管理员身份运行”,再次打开,保存就有效.但总不能每次都那样去右 ...
- SPI 方式初始化 SD 卡总流程图(V2.0)
- backbond整体架构
(function(factory) { // 在这里是backbone模块化的一个接口.支持AMD,CMD和全局变量模式.代码很好理解. })(function(root, factory, _, ...
- Opencv-python画图基础知识
相关函数介绍 1. Point 该数据结构表示了由其图像坐标 和 指定的2D点.可定义为: Point pt; pt.x = 10; pt.y = 8; 或者 Point pt = Point(10, ...
- IdentityServer开题篇
最近也研究了一段时间的IdentityServer4,园里关于IdentityServer的文章也很多,这里也简单写写,做做记录(文笔不佳,见谅). 1.identityserver是什么? Iden ...