Spring(三):使用java config配置spring mvc

一.配置DispatcherServlet
通常使用web.xml配置,此处使用java代码配置
SpittrWebAppInitializer.java

package spittr.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class SpittrWebAppInitializer 
        extends AbstractAnnotationConfigDispatcherServletInitializer{

    @Override
    protected String[] getServletMappings(){
        return new String[]{"/"};
    }

    @Override
    protected Class<?>[] getRootConfigClasses(){
        return new Class<?>[] {RootConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses(){
        return new Class<?>[] {WebConfig.class};
    }
}

spring会自动查找继承AbstractDispatcherServletInitializer的类用来配置servlet容器,AbstractAnnotationConfigDispatcherServletInitializer继承了AbstractDispatcherServletInitializer,所以spring会自动查找到SpittrWebAppInitializer,用来配置servlet;
getServletMappings表示将/为前缀的路径映射到此servlet;getServletConfigClasses方法返回的类,DispatchServlet加载应用上下文时,会使用这些配置类中的bean;getRootConfigClasses方法返回的类,当ContextLoaderListener创建上下文时,会加载这些类中配置的bean;

二.配置spring mvc
WebConfig.java

package spittr.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;


@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "spittr.web")
public class WebConfig  extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver resolver = 
                new InternalResourceViewResolver();

        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);

        return resolver;
    }

    @Override 
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer){
        configurer.enable();
    }

}

@Configuration注解标识这是一个配置类,@EnableWebMvc注解启用spring mvc,@ComponentScan注解配置要扫描的组件,根据此配置来查找控制器;viewResolver方法,返回jsp视图解析器,用于映射jsp;configureDefaultServletHandling方法配置默认的sevlet,当请求匹配不到DispatchServlet的映射路径时,会使用默认的Servlet;

ContextLoaderListener使用的配置:
RootConfig.java

package spittr.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan(basePackages={"spitter"},
                excludeFilters={
                                @Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)
                })
public class RootConfig {

}

三.编写控制器
HomeController.java

package spittr.web;

import static org.springframework.web.bind.annotation.RequestMethod.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {
    @RequestMapping(value="/", method=GET)
    public String home(){
        return "home";
    }
}

@Controller注解基于@Component,会产生一个bean,此控制器的方法home根据注解@RequestMapping(value=”/”, method=GET),匹配到url的/,方法是GET时,返回home,视图解析器,根据home,找到/WEB-INF/views/home.jsp返回;需要添加文件/WEB-INF/views/home.jsp
/WEB-INF/views/home.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
    <title>Spittr</title>
    <link rel="stylesheet"
            type="text/css"
            href="<c:url value="/resources/style.css" />" >

</head>

<body>
    <h1>Welcome to Spitter</h1>

    <a href="<c:url value="/spittles" />">Spittles</a>
    <a href="<c:url value="/spitter/register" />">Register</a>
</body>
</html>

注意,解析此jsp文件依赖jar包jstl.jar、standard.jar,需要放到tomcat的lib或者应用的lib目录下;

四.测试
在tomcat的webapps下创建目录spittr,把类copy到webapps\spittr\WEB-INF\classes下,home.jsp文件copy到webapps\spittr\WEB-INF\views\home.jsp,启动tomcat
浏览器访问http://127.0.0.1:8080/spittr/
显示

Welcome to Spitter

Spittles Register

java config和xml配置的对应关系,参考Servlet 3 + Spring MVC零配置:去除所有xml

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值