所谓番外,就是对之前的项目1进行了改动,比如时间格式化
格式化时间
图解
步骤
- 创建一个规范时间格式的工具类
DateConverter
package com.offcn.utils;
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> {
/*s:传过来的时间*/
@Override
public Date convert(String s) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
/*规范时间的规则(false 当出错误时,不会直接给你报错)*/
sdf.setLenient(false);
try {
/*把string类型的时间转化成时间类型*/
return sdf.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
- 配置xml文件
springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--当你的请求地址为index.HTML时,就回到UserController-->
<!--<bean name="/index.html" class="com.offcn.controller.UserController"></bean>-->
<!--扫描controller下所有的包-->
<context:component-scan base-package="com.offcn.controller"/>
<!--开启注解驱动-->
<mvc:annotation-driven/>
<mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/>
<!-- 配置Conveter转换器 转换工厂 (日期、去掉前后空格)。。 -->
<bean id="conversionServiceFactoryBean" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 配置 多个转换器-->
<property name="converters">
<list>
<bean class="com.offcn.utils.DateConverter"/>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
- 创建一个pojo类
Product
、User
、UserListItem
Product
package com.offcn.pojo;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
import java.util.Date;
public class Product implements Serializable {
private String pname;
private String price;
/*@DateTimeFormat(pattern = "yyyy-mm-dd")*/
private Date pdate;
public Date getPdate() {
return pdate;
}
public void setPdate(Date pdate) {
this.pdate = pdate;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
User
package com.offcn.pojo;
import java.io.Serializable;
public class User implements Serializable {
private int uid;
private String uname;
private String upwd;
private Product product;
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
}
UserListItem
package com.offcn.pojo;
import java.io.Serializable;
import java.util.List;
public class UserListItem implements Serializable {
private List<User> userList;
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
}
- 创建一个控制层
UserController
package com.offcn.controller;
import com.offcn.pojo.Product;
import com.offcn.pojo.User;
import com.offcn.pojo.UserListItem;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
/*
*第一种方法:实现Controller接口
*/
@Controller
/*@RequestMapping("/userController")*/
public class UserController {
/*8080/userController/index*/
/*把页面的数据放到controller*/
/*代表请求的路径*/
/*有一个参数的注解*/
/*userName:第一个为这个代表你参数的键,第二个为值*/
/*method = RequestMethod:设置请求方式*/
/*@RequestMapping(value = "/index",params = {"userName"},method = RequestMethod.GET)*/
/*value = "/index/*.do":只要是index下面以.do结尾的都能匹配到*/
// @RequestMapping(value = "/index/*.do",params = {"userName"},method = RequestMethod.GET)
/*value 代表网页中拼接的键;required 代表是否必须拼接这个参数,false代表可以(或者不可以)拼接 true必须拼接*/
/*PathVariable:匹配uri上的任何参数,注解上的pid跟@RequestMapping(value ="")中的值一样*/
@RequestMapping(value = "/index/{pid}")
// public String index(@RequestParam(value = "userName",required = false) String userName){
// public String index(String userName){
public String index(@PathVariable String pid){
/*jsp——>Control*/
System.out.println(pid);
/*返回的界面*/
return "dolndex";
}
/*@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
return new ModelAndView("dolndex");
}*/
/*Control——>jsp*/
@RequestMapping("/toDolndex")
/*@RequestParam:注解可要可不要*/
public ModelAndView toDolndex(@RequestParam(value = "userName",required = false) String userName){
/*构建modelAndView*/
ModelAndView modelAndView = new ModelAndView();
/*把值放入modelAndView(以键值对的形式来传递)*/
modelAndView.addObject("userName",userName);
/*把跳转界面放到modelAndView里*/
modelAndView.setViewName("dolndex");
System.out.println("1234567667890");
return modelAndView;
}
/*返回要跳转的地址 view Model,这个Model不用实例化,直接传进去*/
@RequestMapping("/toDolndex1")
public String toDolndex1(String userName, Model model){
/*Model里面添加数据,以键值对的形式来传递*/
model.addAttribute("userNmae",userName);
System.out.println(userName);
return "dolndex";
}
/*通过map把数据传过去*/
@RequestMapping("/toDolndex2")
public String toDolndex2(String userName, Map<String,Object> map){
map.put("userName",userName);
return "dolndex";
}
/*
* 最终版
*
* */
@RequestMapping("/toUpdata")
public String toUpdata(
@RequestParam(value = "userName",required = false) String userName,
@RequestParam(value = "userPwd",required = true) String userPwd){
return "dolndex";
}
/*返回要跳转的地址 view Model,这个Model不用实例化,直接传进去*/
@RequestMapping("/toDolndex3")
public String toDolndex3(String userName, Model model){
/*Model里面添加数据,以键值对的形式来传递*/
model.addAttribute("userNmae",userName);
System.out.println(userName);
return "dolndex";
}
@RequestMapping("/addUser")
public String toIndex(User user,Model model){
model.addAttribute("user",user);
return "dolndex";
}
@RequestMapping("/addUser1")
public String toIndex(UserListItem userListItem, Model model){
model.addAttribute("userListItem",userListItem);
return "dolndex";
}
@RequestMapping("/addUser2")
public String toIndex(@RequestParam("ids") List<Object> list,Model model){
model.addAttribute("list",list);
return "dolndex";
}
@RequestMapping("/addUser3")
public String toIndex(Model model){
Product product = new Product();
product.setPdate(new Date());
model.addAttribute(product);
return "dolndex";
}
/*ResponseBody:不是跳转的界面*/
@RequestMapping("/addUser4")
// @ResponseBody
public void toIndex(HttpServletResponse response){
try {
response.getWriter().write("<h1>"+"admin"+"</h1>");
} catch (IOException e) {
e.printStackTrace();
}
// return "asdasd";
}
/*返回:路径 、modelAndView*/
@RequestMapping("/addUser4")
// @ResponseBody
public void toIndex(HttpServletResponse response,HttpServletRequest request){
try {
response.getWriter().write("<h1>"+"admin"+"</h1>");
} catch (IOException e) {
e.printStackTrace();
}
// return "asdasd";
}
}
- 在WEB-INF下新建一个jsp文件夹,并创建一个时间格式化的页面
dolndex.jsp
<%--
Created by IntelliJ IDEA.
User: Dk502可分
Date: 2018/10/16
Time: 10:07
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<fmt:formatDate value="${product.pdate}"></fmt:formatDate>
<%--${userListItem.userList[0].uname}
${userListItem.userList[0].upwd}
${userListItem.userList[0].product.pname}
${userListItem.userList[0].product.price}--%>
<%--${user.uid}
${user.uname}
${user.upwd}
${user.product.pname}
${user.product.price}--%>
</body>
</html>
注意
这只是代码片段,请结合这篇文章SpringのMVC