除暖

基本配置-注解驱动 04

电脑版发表于:2022/7/29 15:47




案例、
1、创建一个配置springMVC的类,然后过滤bean,和配置放行静态资源

2、自定义servlet容器初始化配置类,加载springMvc核心配置类。就是配置web.xml的内容

2.1、

上面的作用就好比在web.xml的这个:

<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.bak</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

2.2、配置中文乱码问题
格式和备注

案例、

代码:

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    super.onStartup(servletContext);
    CharacterEncodingFilter cef = new CharacterEncodingFilter();
    cef.setEncoding("UTF-8");
    FilterRegistration.Dynamic registration = servletContext.addFilter("characterEncodingFilter", cef);
    registration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST,DispatcherType.FORWARD,DispatcherType.INCLUDE),false,"/*");
}

此时,spring-mvc.xml和web.xml都可以删了
测试路径:http://localhost:8080/
http://localhost:8080/spring-mvc.jsp
http://localhost:8080/success.jsp
注意:请求和响应还没怎么配

关于TNBLOG
TNBLOG,技术分享。技术交流:群号677373950
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
{{item.replyName}}
{{item.content}}
{{item.time}}
{{subpj.replyName}}
@{{subpj.beReplyName}}{{subpj.content}}
{{subpj.time}}
猜你喜欢