spring boot 学习--08---搭建ssmm-02

18 篇文章 1 订阅
12 篇文章 5 订阅

接上篇http://blog.csdn.net/javastudyr/article/details/52585770
这篇开始来配置springmvc,并测试

思路:
- 在web.xml配置servlet
- 创建springmvc配置文件
- 创建TestController
- 创建测试页面
- 基本测试
- 乱码测试
- 时间测试

(1)web.xml配置springmvc

<!-- springmvc servlet配置 -->

    <servlet>
        <servlet-name>front</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>front</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

(2)创建spring-mvc.xml 文件
springmvc 配置扫描,自动装配,JSP解析


    <context:component-scan base-package="com.study" use-default-filters="false">
        <!--扫描 cn.itcast下的 所有类中  注解是 Controller的类 -->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!--自动装配 -->
    <context:annotation-config/>

    <!-- Jsp的视图解析器 -->
    <bean id="jspViewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

(3)创建TestController

package com.study.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.study.bean.TestTb;

Controller
public class TestController {

    @RequestMapping("/test/springmvc/testTbAdd")
    public String test_testtb(TestTb tb){

        System.out.println(tb.toString());

        return "success";
    }
}

(4)创建测试页面和success页面,src/main/webapp/下创建index.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="test/springmvc/testTbAdd.do" method="POST">
        名称:<input type="text" name="name"/><br/>
            <input type="submit" value="提交"/>
    </form>

</body>
</html>

在webapp/WEB-INF/view/下创建success.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>
    添加成功!
</body>
</html>

(5)开始基本测试
访问http://localhost:8080/boot-study-ssmm-xml/

这里写图片描述

基本测试结果:
这里写图片描述

(6)在TestController中添加方法,开始中文测试

@RequestMapping(value="/test/unicode",produces = "plain/html; charset=UTF-8")
    public @ResponseBody String test_unicode(){



        return "成功";
    }

访问:
http://localhost:8080/boot-study-ssmm-xml/test/unicode.do

中文测试返回结果:
这里写图片描述

解决办法
1. 在web.xml 中添加字符拦截器,注意加载到所有servlet,filter的前面


    <filter>
        <filter-name>encoding</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>encoding</filter-name>
        <url-pattern>*.do</url-pattern>
    </filter-mapping>

中文测试继续测试
这里写图片描述

这是为什么喃,我们加载了字符拦截器,好像还是不行,但是在访问http://localhost:8080/boot-study-ssmm-xml/ 这个接口的时候输入中文,后台是可以接收了
这里写图片描述

继续解决我们直接返回中文字符串的问题,这里是因为@ResponseBody 注解,这个注解返回的String,springmvc会直接用org.springframework.http.converter.StringHttpMessageConverter 来进行转发,这个默然的编码是:ISO-8859-1
这里写图片描述

明白了,现在我们可以在springmvc中配置这个StringHttpMessageConverter 的编码

<bean  class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >    
    <property name="messageConverters">    
         <list>    
             <bean class = "org.springframework.http.converter.StringHttpMessageConverter">    
                <property name = "supportedMediaTypes">    
                     <list>    
                         <value>text/plain;charset=UTF-8</value>    
                     </list>    
                </property>    
             </bean>    
         </list>    
    </property>    
</bean>

中文测试继续测试2

结果:
这里写图片描述

(7)时间测试

index.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="test/springmvc/testTbAdd.do" method="POST">
        名称:<input type="text" name="name"/><br/>
        生日:<input type="text" name="birthday"/> 
            <input type="submit" value="提交"/>
    </form>

</body>
</html>

时间测试:
这里写图片描述

400,错误,估计就是时间类型不匹配,我们在TestController中绑定一个时间解析来试试

    /**
     * 注册日期格式
     * @param request
     * @param binder
     */
    @InitBinder
    public void InitBinder(HttpServletRequest request ,ServletRequestDataBinder binder){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(df, false));
    }

时间测试1
这里写图片描述

OK,到此所有的都大功告成

补充一下:时间转换器也可以全区配置,在springmvc中,配置如下:

<!-- 全局时间配置 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="webBindingInitializer">
    <!-- 自定义事件处理器 -->
        <bean  class= "com.study.util.BindindInitializer" />
    </property>
</bean>
package com.study.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;

public class BindindInitializer implements WebBindingInitializer{

    public void initBinder(WebDataBinder binder, WebRequest request) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(df, false));
    }

}

下篇就正式进入springboot的开发了

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值