package cn.itcast.converter;

 import org.springframework.core.convert.converter.Converter;

 import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; public class DateConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}

DateConverter

 package cn.itcast.domain;

 public class User {
private String name;
private int age; public User(){} 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;
} @Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

User

 package cn.itcast.vo;

 import cn.itcast.domain.User;

 public class VO {
private User[] userList; public VO(){} public User[] getUserList() {
return userList;
} public void setUserList(User[] userList) {
this.userList = userList;
}
}

VO

 package cn.itcast.web.controller;

 import cn.itcast.domain.User;
import cn.itcast.vo.VO;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.support.BindingAwareModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List; @Controller
//@Component
public class UserController {
/*
响应处理,跳转
*/
@RequestMapping("/quick1")
public String quick1(){
//视图字符串,可以使用绝对路径和相对路径
return "/mvc.jsp";
} @RequestMapping("/quick2")
public ModelAndView quick2(){
ModelAndView modelAndView = new ModelAndView(); //ViewName即视图字符串
modelAndView.setViewName("/mvc.jsp"); //SpringMVC会自动将ModelAndView中存入的数据,存入request域
modelAndView.addObject("name", "刘备"); return modelAndView;
} @RequestMapping("/quick3")
//由SpringMVC创建ModelAndView
public ModelAndView quick3(ModelAndView modelAndView){
//ViewName即视图字符串
modelAndView.setViewName("/mvc.jsp"); //SpringMVC会自动将ModelAndView中存入的数据,存入request域
modelAndView.addObject("name", "孙权"); return modelAndView;
} //使用Model+视图字符串,则Model必须由SpringMVC创建。
@RequestMapping("/quick4")
public String quick4(Model model){
//SpringMVC会自动将ModelAndView中存入的数据,存入request域
model.addAttribute("name", "曹操");
return "/mvc.jsp";
} //错误示例: 返回视图字符串,自己创建Model的话无法交给SpringMVC。
@RequestMapping("/quick5")
public String quick5(){
Model model = new BindingAwareModelMap();
//SpringMVC会自动将ModelAndView中存入的数据,存入request域
model.addAttribute("name", "小乔");
return "/mvc.jsp";
} /*
直接响应数据
*/
@RequestMapping("/quick6")
@ResponseBody
public String quick6(){
return "你好 mvc";//?? mvc
} @RequestMapping("/quick7")
@ResponseBody
public User quick7(){
User user = new User();
user.setName("刘备");
user.setAge(20); return user;
} @RequestMapping("/quick8")
@ResponseBody
public List<User> quick8(){
User user = new User();
user.setName("刘备");
user.setAge(20); User user2 = new User();
user2.setName("孙权");
user2.setAge(20); List<User> userList = new ArrayList<>();
userList.add(user);
userList.add(user2); return userList;
} @RequestMapping("/quick9")
public String quick9(){
return "mvc";
} /*
请求处理
*/
@RequestMapping("/quick10")
@ResponseBody
public void quick10(String name, Integer age){
System.out.println(name + ":" + age);
} @RequestMapping("/quick11")
@ResponseBody
public void quick11(User user){
System.out.println(user);
} @RequestMapping("/quick12")
@ResponseBody
public void quick12(String[] strs){
System.out.println(Arrays.toString(strs));
} @RequestMapping("/quick13")
@ResponseBody
public void quick13(VO vo){
System.out.println(Arrays.toString(vo.getUserList()));
} /* @RequestMapping("/quick14")
@ResponseBody
public void quick14(@RequestBody List<User> userList){
System.out.println(userList);
}*/ @RequestMapping("/quick14")
@ResponseBody
public void quick14(@RequestBody User[] users){
System.out.println(Arrays.toString(users));
} @RequestMapping("/quick15")
@ResponseBody
public void quick15(@RequestParam("name") String username){
System.out.println(username);
} @RequestMapping("/quick16")
@ResponseBody
public void quick16(Date date){
System.out.println(date);
}
}

UserController

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
"> <!--组件扫描-->
<context:component-scan base-package="cn.itcast.web.controller"></context:component-scan> <!--启用jackson-->
<!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
</list>
</property>
</bean>--> <!--设置视图字符串前后缀-->
<!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>--> <!--当SpringMVC没有匹配成功使用的Servlet处理器-->
<!--<mvc:default-servlet-handler></mvc:default-servlet-handler>--> <mvc:resources mapping="/js/**" location="/js/"></mvc:resources> <!--注入自定义类型转换器-->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="cn.itcast.converter.DateConverter"></bean>
</list>
</property>
</bean> <!--mvc注解驱动,默认启用jackson,使用类型转换服务-->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> </beans>

spring-mvc.xml

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>cn.itcast</groupId>
<artifactId>spring_mvc_day2</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<!--springmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency> <!--jackson-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
</dependencies> </project>

pom.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"> <!--SpringMVC核心控制器-->
<servlet>
<servlet-name>DispatcherServlet</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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</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>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>

web.xml

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>你好 mvc ${name}</h1>
</body>
</html>

mvc.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
<script src="/js/jquery-3.3.1.js"></script>
<script>
$(function () {
var users = new Array();
users.push({"name": "刘备"});
users.push({"name": "孙权", age: 20});
//发送异步请求,携带对象数据
$.ajax({
type: "POST",
url: "/quick14",
data: JSON.stringify(users),
contentType: "application/json;charset=utf-8"
});
});
</script>
</head>
<body> </body>
</html>

ajax.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/quick13" method="post">
<input type="text" name="userList[0].name">
<input type="text" name="userList[0].age">
<input type="text" name="userList[1].name">
<input type="text" name="userList[1].age">
<input type="submit" value="提交">
</form>
</body>
</html>

array.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/quick15" method="post">
<input type="text" name="name">
<input type="submit" name="提交">
</form>
</body>
</html>

encode.jsp

SpringMVC入门 bug集锦X2的更多相关文章

  1. SpringMVC入门 bug集锦X3和SSM原始整合

  2. MyBatis入门Bug集锦X1

  3. 【Spring】SpringMVC入门示例讲解

    目录结构: // contents structure [-] SpringMVC是什么 Spring MVC的设计原理 SpringMVC入门示例 1,复制Jar包 2,Web.xml文件 3,My ...

  4. Spring+SpringMVC+MyBatis深入学习及搭建(十二)——SpringMVC入门程序(一)

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6999743.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十一)——S ...

  5. SpringMVC入门第二天

    SpringMVC第二天 1.   回顾 1.Springmvc介绍? Springmvc是Spring公司 2.Springmvc入门程序 第一步:Web工程 第二步:导Jar包 第三步:web.x ...

  6. 3.2.1 SpringMVC入门

    一. SpringMVC入门 1. MVC介绍 MVC全名 是Model View Controller, 是模型(model) - 视图(view) - 控制器(controller) 的缩写, 它 ...

  7. SpringMVC入门学习(二)

    SpringMVC入门学习(二) ssm框架 springMVC  在上一篇博客中,我简单介绍了一下SpringMVC的环境配置,和简单的使用,今天我们将进一步的学习下Springmvc的操作. mo ...

  8. SpringMVC入门学习(一)

    SpringMVC入门学习(一) ssm框架 spring  SpringMVC是一个Java WEB框架,现在我们知道Spring了,那么,何为MVC呢? MVC是一种设计模式,其分为3个方面 mo ...

  9. springMVC入门(一)

    1.   SpringMVC入门 1.1SpringMVC是什么 Spring web mvc和Struts2都属于表现层的框架,它是Spring框架的一部分,我们可以从Spring的整体结构中看得出 ...

随机推荐

  1. ubuntu16.04固定IP与设置DNS【转】

    本文转载自:http://www.jianshu.com/p/ea4bca38e5d7 学校几乎每次断电之后,宿舍的机器IP基本都会更改,即使可以通过图形界面去修改,但还是懒得去改.但是docker某 ...

  2. 跟我一起学Git (十) Patches【转】

    本文转载自:http://cs-cjl.com/2014/05/05/learn_git_with_me_10 Git实现了以下三条用于交换patch的命令: git format-patch 用于创 ...

  3. SpringBoot2.0之整合ActiveMQ(发布订阅模式)

    发布订阅模式与前面的点对点模式很类似,简直一毛一样 注意:发布订阅模式 先启动消费者 公用pom: <project xmlns="http://maven.apache.org/PO ...

  4. python增删改查zabbix主机等

    摘自: http://www.jianshu.com/p/e087cace8ddf 一.API简介 Zabbix API是在1.8版本中开始引进并且已经被广泛应用.所有的Zabbix移动客户端都是基于 ...

  5. python读取文件后切片

    from itertools import islice with open(“1.txt") as f: for a in islice(f,0,2): print(a)

  6. jquery 3D分页翻转滑块

    jquery 3D分页翻转滑块,jquery分页,jquery插件,jquery,3D翻转,css3分页,360度旋转,网页特效代码3D分页翻转滑块是一款使用网格样式与滑块效果分页的特效.

  7. 分享知识-快乐自己:SpringMvc整合遇到-前台传JSON参数,后台实体类对象接收

    前台数据转JSON对象: /** * * @param $myFrom:from表单 * @returns {{}} */ function from($myFrom) { var ebookEntr ...

  8. Anomaly Detection for Time Series Data with Deep Learning——本质分类正常和异常的行为,对于检测异常行为,采用预测正常行为方式来做

    A sample network anomaly detection project Suppose we wanted to detect network anomalies with the un ...

  9. redis实现分布式锁——核心 setx+pipe watch监控key变化-事务

    如何设计一把分布式锁 我们用 redis 来实现这把分布式的锁,redis 速度快.支持事务.可持久化的特点非常适合创建分布式锁. 分布式环境中如何消除网络延迟对锁获取的影响 锁,简单来说就是存于 r ...

  10. C/C++协程的实现方式总结

    1.利用 C 语言的 setjmp 和 longjmp,函数中使用 static local 的变量来保存协程内部的数据. 函数原型:int setjmp(jmp_buf envbuf); void  ...