在之前的Spring 4 MVC HelloWorld - 实例中,有spring-mvc.xml和web.xml这两个xml文件。现在通过java配置,将删除这个两个xml文件。
1、pom.xml文件
<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.demo.springmvc</groupId> <artifactId>Spring4MVCHelloWorldDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <springframework.version>4.0.6.RELEASE</springframework.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springframework.version}</version> </dependency> <!-- Below declared dependencies are included for the servers who may complain about servlet/jstl missing dependency --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <!-- 目的是,删除web.xml(不管用,待解决。) --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <warSourceDirectory>src/main/webapp</warSourceDirectory> <warName>Spring4MVCHelloWorldDemo</warName> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </pluginManagement> <finalName>Spring4MVCHelloWorldDemo</finalName> </build> </project>
步骤2:添加控制器
import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/") public class HelloWorldController { @RequestMapping(method = RequestMethod.GET) public String sayHello(ModelMap model) { model.addAttribute("greeting", "Hello World from Spring 4 MVC"); return "welcome"; } @RequestMapping(value = "/helloagain", method = RequestMethod.GET) public String sayHelloAgain(ModelMap model) { model.addAttribute("greeting", "Hello World Again, from Spring 4 MVC"); return "welcome"; } }
步骤4:添加视图
<%@ 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> Greeting:${greeting} </body> </html>
步骤5:添加配置类
package configuration; 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.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; @Configuration @EnableWebMvc @ComponentScan(basePackages = "com.springmvc") public class HelloWorldConfiguration { @Bean public ViewResolver viewResolver(){ InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setViewClass(JstlView.class); viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); return viewResolver; } }
@Configuration @EnableWebMvc
相当于<mvc:annotation-driven />XML,目的是支持@Controller注解的类,用于@RequestMapping
将传入的请求映射到特定的方法。
@ComponentScan(basePackages = "com.springmvc")相当于<context:component-scan base-package="com.springmvc" />
步骤6:添加初始化类
package configuration; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; public class HelloWorldInitializer implements WebApplicationInitializer{ @Override public void onStartup(ServletContext container) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(HelloWorldConfiguration.class); ctx.setServletContext(container); ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx)); servlet.setLoadOnStartup(1); servlet.addMapping("/"); } }
上面的内容类似于上一个教程中的web.xml的内容,因为我们使用前端控制器DispatherServler
,分配映射(xml中的url-pattern),而不是为spring配置文件(spring-mvc.xml)提供路径,这里我们正在注册配置类。总的来说,我们正在做同样的事情,只是方法是不同的。
更新:请注意,现在您可以更简洁地编写上述类[ 这是首选方式 ],通过扩展AbstractAnnotationConfigDispatcherServletInitializer
基类,如下所示:
package configuration; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer{ @Override protected Class<?>[] getRootConfigClasses() { return new Class[]{HelloWorldConfiguration.class}; } @Override protected Class<?>[] getServletConfigClasses() { return null; } @Override protected String[] getServletMappings() { return new String[]{"/"}; } }
部署运行。
时间: 2024-10-14 05:04:29