[译]如何使用Spring's Java Configuration (JavaConfig)

类别: 技术文
**关键词: ** JavaConfig | Spring MVC
原文出处:http://johnathanmarksmith.com/

原文标题为:怎样在Spring-MVC-Web中使用Spring's-Java-Configuration (JavaConfig)
标题在打出来有点长……囧

SpringMVC-helloworld

这是一个非常基础的在Spring-MVC中使用JavaConfig来编写helloworld web程序的例子.

第一部分我来为我们的web-app创建一个配置类(configuaration class)。下面这个就是我们即将使用的configuaration class案例:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.johnathanmsmith.mvc.web"})
public class WebMVCConfig extends WebMvcConfigurerAdapter
{

    private static final Logger logger = LoggerFactory.getLogger(WebMVCConfig.class);

    @Bean
    public ViewResolver resolver()
    {
        UrlBasedViewResolver url = new UrlBasedViewResolver();
        url.setPrefix("/views/");
        url.setViewClass(JstlView.class);
        url.setSuffix(".jsp");
        return url;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        logger.debug("setting up resource handlers");
        registry.addResourceHandler("/resources/").addResourceLocations("/resources/**");
    }

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

    @Bean
    public SimpleMappingExceptionResolver simpleMappingExceptionResolver()
    {
        SimpleMappingExceptionResolver b = new SimpleMappingExceptionResolver();

        Properties mappings = new Properties();
        mappings.put("org.springframework.web.servlet.PageNotFound", "p404");
        mappings.put("org.springframework.dao.DataAccessException", "dataAccessFailure");
        mappings.put("org.springframework.transaction.TransactionException", "dataAccessFailure");
        b.setExceptionMappings(mappings);
        return b;
    }
}

接下来,你需要设置web.xml文件来使用上面的配置类,当然我们还需要来为contectConfigLocation属性设置配置类所在的包(路径)。设置如下:



    
        Spring MVC Dispatcher Servlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextClass
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        
        
            contextConfigLocation
            com.johnathanmsmith.mvc.web.config, com.johnathanmsmith.mvc.web.controller
        
        1
    

    
        Spring MVC Dispatcher Servlet
        /
    

好的,下面我们设置一个基础控制器来展示下页面看看:

@Controller
@RequestMapping("/ask")
class IndexController
{

    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    @RequestMapping(method = RequestMethod.GET)
    public String displayRequestPage()
    {
        /*
           I am going to display the helloworld.jsp page now :)
         */
        logger.debug("made it to controller");
        return "helloworld";

    }

}

好的,到这里就全部就绪了...

获取这个项目并运行

要获得这个项目并运行它的话,你需要按照下面的步骤进行:

git clone  [email protected]:JohnathanMarkSmith/springmvc-helloworld.git
cd springmvc-helloworld/
mvn tomcat7:run

在此之后你就可以打开浏览器然后输入地址:http://127.0.0.1:8080/springmvc-helloworld/

嗯,运行起来就是这样了。

这里有我所做的视频(视频地址即原文地址)

如果你有任何问题或者建议可以给我[email protected]发邮件

欢迎指正译文错误,不胜感激!

你可能感兴趣的:([译]如何使用Spring's Java Configuration (JavaConfig))