Spring4 In Action-5.2.2-Spring Web应用程序-简单的控制器实现跳转

Posted 第二天半

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring4 In Action-5.2.2-Spring Web应用程序-简单的控制器实现跳转相关的知识,希望对你有一定的参考价值。

Spring4 In Action-5.2.2-Spring Web应用程序-简单的控制器实现跳转

// 源码下载地址:http://download.csdn.net/download/poiuy1991719/10111794

 

首先创建启动配置的类:

package spittr.config;

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

import spittr.web.WebConfig;

/***
 * 容器会找到AbstractAnnotationConfigDispatcherServletInitializer这个类(包括这个类的扩展)
 * 然后用这个类来配置Servlet上下文
 */
public class SpitterWebInitializer extends
        AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {//非Web组件,带有@Configuration注解的类用来定义应用上下文
        return new Class<?>[] { RootConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {//指定配置类,带有@Configuration注解的类用来定义应用上下文
        return new Class<?>[] { WebConfig.class };
    }

    @Override
    protected String[] getServletMappings() {// 将DispatcherServlet映射到"/"
        return new String[] { "/" };
    }

}

 

然后编写WebConfig配置类,代替以前的xml配置

package spittr.web;

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.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
/***
 * 配置web
 */
@Configuration //配置类,需要的注解
@EnableWebMvc  //启用Spring MVC
@ComponentScan("spittr.web")  //启用组件扫描,就会扫描到控制器
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver viewResolver() {//配置JSP视图解析器
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");//在请求路径前,加入资源路径
        resolver.setSuffix(".jsp");//在请求路径后,加入资源名称,比如/WEB-INF/views/home.jsp
        resolver.setViewClass(org.springframework.web.servlet.view.JstlView.class);
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(  //处理静态资源
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        super.addResourceHandlers(registry);
    }

}

 

编写Root配置类:可以使用其它非Web组件,比如数据库连接:

package spittr.config;

import java.util.regex.Pattern;

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.context.annotation.Import;
import org.springframework.core.type.filter.RegexPatternTypeFilter;

import spittr.config.RootConfig.WebPackage;

/***
 * 引入数据库配置类 自动扫描spittr包,以及子包
 */
@Configuration  // 配置类,需要的注解
@Import(DataConfig.class)
@ComponentScan(basePackages = { "spittr" }, excludeFilters = { @Filter(type = FilterType.CUSTOM, value = WebPackage.class) })
public class RootConfig {
    public static class WebPackage extends RegexPatternTypeFilter {
        public WebPackage() {
            super(Pattern.compile("spittr\\.web"));
        }
    }
}

 

数据库连接配置:

package spittr.config;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

@Configuration
public class DataConfig {

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql:///spittle");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        
        return dataSource;
    }

    @Bean
    public JdbcOperations jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

}

 

网站应用,基本配置已经OK了,编写控制器:

package spittr.web.spittle;

import static org.springframework.web.bind.annotation.RequestMethod.*;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/")
// 处理所有主路径"/"的请求
public class HomeController {

    @RequestMapping(method = GET)
    public String home(Model model) {
        return "home";// "home.jsp":因为在WebConfig类里面,设置了自动加入".jsp",所以对应的是"home.jsp"
    }

}

 

创建页面:(路径:/WEB-INF/views/home.jsp)

<html>
<head>
<title>Spitter</title>
</head>
<body>
    <h1>Welcome to Spitter</h1>
</body>
</html>

 

以上是关于Spring4 In Action-5.2.2-Spring Web应用程序-简单的控制器实现跳转的主要内容,如果未能解决你的问题,请参考以下文章

Spring4 In Action-5.2.2-Spring Web应用程序-简单的控制器实现跳转

Spring4 In Action-5.2.3-Spring Web应用程序-向页面输出列表接收参数接收表单

Spring4相关jar包介绍(转)

Spring版本功能变更&Spring4.x的新特性

spring in action 4 第6章 视图分发

Spring4.1新特性——Spring缓存框架增强(转)