SpingMvc 基于java 配置(0配置)
来源:互联网 发布:如何在淘宝发布产品 编辑:程序博客网 时间:2024/06/11 02:11
3个文件 配置 最简单的SpringMVC
1创建WebAppInitializer
package com.sp.demo2;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.ServletRegistration.Dynamic;import org.springframework.web.WebApplicationInitializer;import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;import org.springframework.web.servlet.DispatcherServlet;public class WebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(WebConfig.class); ctx.setServletContext(servletContext); //2 Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); //3 servlet.addMapping("/"); servlet.setLoadOnStartup(1); servlet.setAsyncSupported(true);//1 }}
2创建WebConfig
package com.sp.demo2;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("com.sp.demo2")public class WebConfig extends WebMvcConfigurerAdapter { @Bean public ViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/jsp/"); viewResolver.setSuffix(".jsp"); viewResolver.setExposeContextBeansAsAttributes(true); return viewResolver; } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { // TODO Auto-generated method stub configurer.enable(); }}
3创建HomeController
package com.sp.demo2;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@Controllerpublic class HomeController { @RequestMapping(value = "/", method = RequestMethod.GET) public String home() { return "home"; }}
4创建/jsp/home.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>Spring MVC </body></html>
运行 就 跳到对应的页面了。
代码下载
0 0
- SpingMvc 基于java 配置(0配置)
- SpingMVC 修改配置路径
- SpingMVC入门-简单配置
- javaWeb中配置spingMVC
- Spring 基于Java配置
- 基于 java 配置Spring
- SpingMvc配置string类型自动转换date日期类型
- spring基于java的配置
- spring 基于java的配置
- MyBatis基于Java API配置
- 基于java配置的springMvc
- Spring基于java注解配置
- spring-基于java的配置
- 基于 Java 的 bean 配置
- [Spring] 基于 Java 的配置
- Spring 基于XML配置 基于注解配置 基于JAVA类配置比较以及适用场景
- SpringMVC入门之二:配置及启用SpringMVC(基于JAVA配置)
- 120330 selenium RC 基于java 环境配置
- 跨路径读取Cookie
- (0013)iOS 开发之集成友盟第三方登录
- Android笔记:Android获取系统时间的几种方法
- Jfinal与shiro集成实现动态URL鉴权,不装插件只需要一个类
- 2016.12.27_IIC的使用总结
- SpingMvc 基于java 配置(0配置)
- Android 镜像使用帮助
- Android HTTP和HTTPS那点事
- linux----which命令
- zTree插件setting配置详解
- 在禁用UAC时无法激活此应用
- wsimport 带https协议wdsl 时报错的处理
- 去掉ogre引擎设置的窗口
- 责任链模式(行为型)