继续

继上节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>
&nbsp;
<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 试水的更多相关文章

  1. springmvc复习笔记----springmvc最简单的第一个例子:RequestMapping试水

    结构 用到的包 web.xml <url-pattern>/</url-pattern>中可以换成其他的后缀*.do ,*. sb  …… <?xml version=& ...

  2. springmvc复习笔记----文件上传multipartResolver

    结构                                              web.xml <?xml version="1.0" encoding=&q ...

  3. springmvc复习笔记----Restful 风格,PathVariable获取 Url实例

    结构 包与之前相同 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&qu ...

  4. 【转载】SpringMVC学习笔记

    转载于:SpringMVC笔记 SpringMVC 1.SpringMVC概述 MVC: Model(模型): 数据模型,提供要展示的数据,:Value Object(数据Dao) 和 服务层(行为S ...

  5. SpringMVC学习笔记之二(SpringMVC高级参数绑定)

    一.高级参数绑定 1.1 绑定数组 需求:在商品列表页面选中多个商品,然后删除. 需求分析:功能要求商品列表页面中的每个商品前有一个checkbok,选中多个商品后点击删除按钮把商品id传递给Cont ...

  6. springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定

    springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定 标签: springmvc springmvc学习笔记13-springmvc注解开发之集合类型參数绑定 数组绑定 需 ...

  7. 史上最全的SpringMVC学习笔记

    SpringMVC学习笔记---- 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于Spring ...

  8. springmvc学习笔记--REST API的异常处理

    前言: 最近使用springmvc写了不少rest api, 觉得真是一个好框架. 之前描述的几篇关于rest api的文章, 其实还是不够完善. 比如当遇到参数缺失, 类型不匹配的情况时, 直接抛出 ...

  9. springmvc学习笔记---面向移动端支持REST API

    前言: springmvc对注解的支持非常灵活和飘逸, 也得web编程少了以往很大一坨配置项. 另一方面移动互联网的到来, 使得REST API变得流行, 甚至成为主流. 因此我们来关注下spring ...

随机推荐

  1. 11张PPT介绍Paxos协议

    之前翻译了<The Part-Time Parliament>一文,论文非常经常,强烈推荐读一读原文.翻译完论文后,希望自己能用简单的描述来整理自己的理解,所以花了一些时间通过PPT的形式 ...

  2. 基础编程复习:输出n以内的所有素数

    暴力遍历:对于1~n以内的每一数i 每一个i只需要考虑2~i开根号以内是否有可以让i整除的数,即(i%x==0)只要满足就不是素数 否则输出 #include<iostream> #inc ...

  3. ClickHouse之clickhouse-local

    一直在慢慢的摸索clickhouse,之前是用rpm包安装的,没有发现clickhouse-local,最近在centos上面编译成功以后发现多了clickhouse-local,那么这个玩意是什么鬼 ...

  4. 【原创】驱动枚举之QueryServiceStatus

    BOOL WINAPI QueryServiceStatus( _In_ SC_HANDLE hService, _Out_ LPSERVICE_STATUS lpServiceStatus ); 函 ...

  5. 使用Laya引擎开发微信小游戏(下)

    本文由云+社区发表 6. 动画 6.1 创建伞兵对象 在src目录下创建一个新目录role,用来存放游戏中角色. 在role里创建一个伞兵Soldier.ts对象文件. module role{ ex ...

  6. systemctl enable docker.service

    [root@dingyingsi ~]# systemctl start docker.service [root@dingyingsi ~]# systemctl enable docker.ser ...

  7. go基础系列:结构struct

    Go语言不是一门面向对象的语言,没有对象和继承,也没有面向对象的多态.重写相关特性. Go所拥有的是数据结构,它可以关联方法.Go也支持简单但高效的组合(Composition),请搜索面向对象和组合 ...

  8. OJ:奇怪的类复制

    描述 程序填空,使其输出9 22 5 #include <iostream> using namespace std; class Sample { public: int v; // 在 ...

  9. SQL--server事物

    事物 特点: 1.原子性:事物必须是一个自动工作的单元, 2.一致性:事物结束的时候,所有内部数据都是正确的 3.隔离性:并发多个事物时,各个事物不干涉内部数据,处理的都是另外一个事物处理之前或之后的 ...

  10. This relative module was not found (转载)

    vue踩坑-This relative module was not found 在使用vue.js的日期选择插件 的时候,报错如下   This relative module was not fo ...