SpringMVC入门 bug集锦X2
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的更多相关文章
- SpringMVC入门 bug集锦X3和SSM原始整合
- MyBatis入门Bug集锦X1
- 【Spring】SpringMVC入门示例讲解
目录结构: // contents structure [-] SpringMVC是什么 Spring MVC的设计原理 SpringMVC入门示例 1,复制Jar包 2,Web.xml文件 3,My ...
- Spring+SpringMVC+MyBatis深入学习及搭建(十二)——SpringMVC入门程序(一)
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6999743.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十一)——S ...
- SpringMVC入门第二天
SpringMVC第二天 1. 回顾 1.Springmvc介绍? Springmvc是Spring公司 2.Springmvc入门程序 第一步:Web工程 第二步:导Jar包 第三步:web.x ...
- 3.2.1 SpringMVC入门
一. SpringMVC入门 1. MVC介绍 MVC全名 是Model View Controller, 是模型(model) - 视图(view) - 控制器(controller) 的缩写, 它 ...
- SpringMVC入门学习(二)
SpringMVC入门学习(二) ssm框架 springMVC 在上一篇博客中,我简单介绍了一下SpringMVC的环境配置,和简单的使用,今天我们将进一步的学习下Springmvc的操作. mo ...
- SpringMVC入门学习(一)
SpringMVC入门学习(一) ssm框架 spring SpringMVC是一个Java WEB框架,现在我们知道Spring了,那么,何为MVC呢? MVC是一种设计模式,其分为3个方面 mo ...
- springMVC入门(一)
1. SpringMVC入门 1.1SpringMVC是什么 Spring web mvc和Struts2都属于表现层的框架,它是Spring框架的一部分,我们可以从Spring的整体结构中看得出 ...
随机推荐
- CodeForces - 960F Pathwalks —— 主席树(n棵线段树)
题目链接:https://vjudge.net/problem/CodeForces-960F You are given a directed graph with n nodes and m ed ...
- Kbuntu16.04添加工作空间
工作空间是一个非常方便的功能,可以让开发者每次只聚焦一个屏幕,又能在各个空间中来回切换.有一种屏幕被扩展的感觉. Kbuntu默认只有一个工作空间,需要按如下方式添加: System settings ...
- 如何去掉ArrayList重复的id
今天,也同样跟你一样寻找这个问题的解决办法, 如何去掉ArrayList重复的id 百度中找到这么一句话,为什么想到要去掉呢?为什么不反过来想,我在往ArrayList里添加的时候不让重复的项加入呢? ...
- L93
Three Days to See(Excerpts) All of us have read thrilling stories in which the hero had only a limit ...
- BZOJ-1568: Blue Mary开公司 (李超线段树)
Description Input 第一行 :一个整数N ,表示方案和询问的总数. 接下来N行,每行开头一个单词“Query”或“Project”. 若单词为Query,则后接一个整数T,表示Blue ...
- CF331B Cats Transport[斜率优化dp+贪心]
luogu翻译 一些山距离起点有距离且不同,m只猫要到不同的山上去玩ti时间,有p个铲屎官人要去把所有猫接走,步行速度为1单位每秒,从1走到N座山不停下,必须在猫玩完后才可以把他带走.可以提前出发.问 ...
- 关于qwerta
性别女 爱好男 有时喜欢装成男孩子混迹于OI圈. 就读于长沙市MD中学 是个剧毒蒻蒻蒻. 以 qwerta['kwɜ:rtɑ] 的ID混迹于各大OJ,但是在其它地方通常用qwertaya(重名率太高了 ...
- bzoj 4319: Suffix reconstruction 后缀数组+构造
题目大意 给定后缀数组sa,要求构造出满足sa数组的字符串.或输出无解\(n\leq 5*10^5\) 题解 我们按照字典序来考虑每个后缀 对于\(Suffix(sa[i])\)和\(Suffix(s ...
- 【Opencv】Mat基础
1.Mat::imread() C++: Mat imread(const string& filename, int flags=1 ) filename – Name of file to ...
- JS---设置简易红绿灯
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...