Spring实战4 学习笔记(三)

学习到Spring实战4这本书的第五章的时候,讲解的是Spring在Web中的应用,一开始的例子是利用注解来配置SpringMvc,完全不需要web.xml文件。

1、首先是SpittrWebAppInitializer类,继承了AbstractAnnotationConfigDispatcherServletInitializer类,这个类约等于在web.xml中干的事情,实现如下:

package spittr.config;

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

public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
    	System.out.println("getRootConfigClasses11");
    	return new Class<?>[] { RootConfig.class };
    }

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

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

需要重写的方法有三个getRootConfigClasses、getServletConfigClasses、getServletMappings,具体每个方法是干什么用的,书里面讲的很清楚了。

另外需要实现RootConfig和WebConfig两个类:

package spittr.config;

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

@Configuration
@ComponentScan(
        basePackages = {"spittr.web"},
        excludeFilters = {@ComponentScan.Filter(
                type = FilterType.ANNOTATION,
                value = EnableWebMvc.class)})
public class RootConfig {
}
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.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
@Configuration
@EnableWebMvc
@ComponentScan("spittr")
public class WebConfig extends WebMvcConfigurationSupport {
    @Bean
    public ViewResolver viewResolver() {
        //閰嶇疆JSP瑙嗗浘瑙f瀽鍣�
    	System.out.println("viewResolver111222");
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        //鍙互鍦↗SP椤甸潰涓�氳繃${}璁块棶beans
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable(); //閰嶇疆闈欐�佹枃浠跺鐞�
    }
}

2、编写一个controller和对应的jsp文件:

package spittr.web;

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

@Controller
public class HomeController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home() {
    	System.out.println("HomeController111");
        return "home";
    }
}
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
    <head>
        <title>Spittr</title>
    </head>
    <body>
        <h1>Welcome to Spittr</h1>
    </body>
</html>

3、这时的目录结构如下,我创建的是一个java web项目:

下面的web.xml和***-servlet.xml都用不上

4、最后用run on server在tomcat上运行本项目,之后就可以在浏览器检查效果了

5、最后说一个我自己遇到的坑,本来整个项目都是在idea里面编写的,后面把class文件粘贴到eclipse中之后,怎么运行都运行不起来,最后发现是idea生成的文件是utf-8字符集的,而eclipse默认的是GBK,所在在项目的里面吧字符集修改了一下就可以运行了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值