Spring3.1 全annotation 配置MVC

去掉web.xml

[java]  view plain copy
  1. /* 
  2.  * To change this template, choose Tools | Templates 
  3.  * and open the template in the editor. 
  4.  */  
  5. package Servlet3;  
  6.   
  7. import Config.AppConfig;  
  8. import Config.DispatcherConfig;  
  9. import javax.servlet.ServletContext;  
  10. import javax.servlet.ServletException;  
  11. import javax.servlet.ServletRegistration;  
  12. import org.springframework.web.WebApplicationInitializer;  
  13. import org.springframework.web.context.ContextLoaderListener;  
  14. import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;  
  15. import org.springframework.web.context.support.XmlWebApplicationContext;  
  16. import org.springframework.web.servlet.DispatcherServlet;  
  17.   
  18. /** 
  19.  * 
  20.  * @author Administrator 
  21.  */  
  22. public class MyWebAppInitializer implements WebApplicationInitializer {  
  23.     @Override  
  24.     public void onStartup(ServletContext container) {  
  25.  AnnotationConfigWebApplicationContext dispatcherContext =new AnnotationConfigWebApplicationContext();  
  26.   dispatcherContext.register(DispatcherConfig.class);       
  27.          //Register and map the dispatcher servlet  
  28.         ServletRegistration.Dynamic dispatcher =container.addServlet("dispatcher"new DispatcherServlet(dispatcherContext));  
  29.         dispatcher.setLoadOnStartup(1);  
  30.         dispatcher.addMapping("*.html");  
  31.     }  
  32. }  
2.配置

[java]  view plain copy
  1. package Config;  
  2. import org.springframework.context.annotation.Bean;  
  3. import org.springframework.context.annotation.ComponentScan;  
  4. import org.springframework.context.annotation.Configuration;  
  5. import org.springframework.web.servlet.config.annotation.EnableWebMvc;  
  6. import org.springframework.web.servlet.view.InternalResourceViewResolver;  
  7. @Configuration//相当于<mvc:annotation-driven/>   
  8. @EnableWebMvc//包含<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>  
  9. @ComponentScan(basePackages={"Controller"})//相当于<context:component-scan base-package="Controller"/>        
  10. public class DispatcherConfig {  
  11.     @Bean  
  12.     public InternalResourceViewResolver viewResolver() {  
  13.         InternalResourceViewResolver resolver = new InternalResourceViewResolver();  
  14.         resolver.setPrefix("/WEB-INF/jsp/");  
  15.         resolver.setSuffix(".jsp");  
  16.         return resolver;  
  17.     }     
  18. }  

3.控制器

[java]  view plain copy
  1. package Controller;  
  2. import org.springframework.stereotype.Controller;  
  3. import org.springframework.web.bind.annotation.RequestMapping;  
  4. @Controller  
  5. public class IndexController {      
  6.     @RequestMapping("index")  
  7.     public String hello(){  
  8.     return "index";      
  9.     }  
  10. }  
4.视图

index.html 

[html]  view plain copy
  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5.         <title>Page</title>  
  6.     </head>  
  7.     <body>  
  8.         <h1>pass</h1>  
  9.     </body>  
  10. </html>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值