如何用Java类配置Spring MVC(不通过web.xml和XML方式)
来源:互联网 发布:win8风格网站源码 编辑:程序博客网 时间:2024/05/16 01:55
建立普通MAVEN项目配置POM
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ghg</groupId> <artifactId>chart04</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <!-- generic --> <java.version>1.7</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- web jsp --> <jsp.version>2.2</jsp.version> <jstl.verison>1.2</jstl.verison> <servlet.version>3.1.0</servlet.version> <!--spring --> <spring-framework.version>4.3.6.RELEASE</spring-framework.version> <!--logging --> <logback.version>1.2.1</logback.version> <slf4j.version>1.7.23</slf4j.version> <log4j.version>1.2.17</log4j.version> </properties> <dependencies> <!-- javax --> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-framework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring-framework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring-framework.version}</version> </dependency> <!--other web dependency --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>${jsp.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl.verison}</version> </dependency> <!-- servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet.version}</version> <scope>provided</scope> </dependency> <!-- logging --> <!-- SLF4J和logback --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <!-- logback --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <!-- jackson --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.6</version> </dependency> </dependencies> <build> <finalName>char04</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.0.0</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> <!-- <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.1.14.v20131031</version> <configuration> <scanIntervalSeconds>600</scanIntervalSeconds> <scanTargetPatterns> <scanTargetPattern> <directory>src/main/webapp</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> <include>**/*.tld</include> </includes> </scanTargetPattern> </scanTargetPatterns> <webAppConfig> <contextPath>/char04</contextPath> </webAppConfig> <connectors> <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> <port>9090</port> <maxIdleTime>60000</maxIdleTime> </connector> </connectors> </configuration> </plugin> --> </plugins> </build></project>
配置DispatcherServlet. 需要创建一个Web初始化类
MyMVCConfig extends WebMvcConfigurerAdapter 继承WebMvcConfigurerAdapter
package com.ghg.chart04.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;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;import org.springframework.web.servlet.view.JstlView;@Configuration@EnableWebMvc@ComponentScan("com.ghg.chart04.controller")public class MyMVCConfig extends WebMvcConfigurerAdapter { @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/classes/views"); resolver.setSuffix(".jsp"); resolver.setViewClass(JstlView.class); resolver.setExposeContextBeansAsAttributes(true); return resolver; } // 配置静态资源的处理 @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); // 对静态资源的请求转发到容器缺省的servlet,而不使用DispatcherServlet }}
配置启动配置类RootConfig
package com.ghg.chart04.init;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.web.servlet.config.annotation.EnableWebMvc;@Configuration@ComponentScan(basePackages={"com.ghg.chart04"},excludeFilters={@Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)})public class RootConfig {}
配置Spring MVC视图解析WebConfig.java, 需要要创建一个类继承自WebMvcConfigurerAdapter
package com.ghg.chart04.init;import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;import com.ghg.chart04.config.MyMVCConfig;public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[] { RootConfig.class }; //启动配置类 } @Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[] { MyMVCConfig.class }; //ָ指定Web配置类 } @Override protected String[] getServletMappings() { return new String[]{"/"}; }}
至此, 传统方式中需要通过web.xml进行配置的东西就已将全部完成有上面三个java类(WebInitializer , RootConfig, MyMVCConfig )完成. 可以开始写Controller和页面代码了
控制器和页面
package com.ghg.chart04.controller;import javax.servlet.http.HttpServletRequest;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloController { @RequestMapping(value="/hello") public Map<String, String> hello(HttpServletRequest request){ Map<String, String> params = new HashMap<String, String>(); params.put("title", "hello"); params.put("result", "中奖了"); return params; }}
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>Welcome</title></head><body><pre>Welcome to Spring MVC!</pre></body></html>
0 0
- 如何用Java类配置Spring MVC(不通过web.xml和XML方式)
- SpringMVC如何用Java类配置(不通过web.xml和XML方式)
- SpringMVC如何用Java类配置(不通过web.xml和XML方式)
- Spring MVC web.xml配置
- 三,基于Java配置、无web.xml文件方式配置Spring MVC
- Spring4 Spring MVC实战(三)——Spring MVC不通过xml配置访问HMTL和其他静态资源
- [spring]Spring MVC web.xml 配置文件配置
- spring mvc 配置web.xml时“/”和“/*”的区别
- 配置pom.xml、spring.xml、spring-mvc.xml、spring-mybatis.xml、web.xml
- spring-mvc.xml文件的配置方式
- spring mvc web.xml的配置
- spring mvc的web.xml的配置
- Spring MVC web.xml 配置文件配置
- Spring MVC的web.xml配置详解
- spring mvc 的web.xml配置
- spring mvc中web.xml的配置
- Spring MVC的web.xml配置详解
- spring MVC 的 web.xml 配置讲解
- iOS Camera(1)
- 第三十章 SpringBoot访问应用参数
- LeetCode-28. Implement strStr()
- 巨头切入 或将改变云计算客服市场
- 图片CenterCrop和圆角问题(Glide加载)
- 如何用Java类配置Spring MVC(不通过web.xml和XML方式)
- 李开复:为什么人工智能是中国很大的机会?
- 第三十一章 SpringBoot使用CommandLineRunner
- 独木舟上的旅行(贪心)
- 【精华分享】【Unity&AI】AI资料总览
- Qt qml pageview 左右滑动分页组件
- 该写点什么了(再次回来写博客)
- 第三十二章 SpringBoot使用application配置文件
- 解决用cocoapod 找不到文件的问题