前一节Spring MVC3入门之1 Hello World(XML based) ,本章同样基于其环境版本。
对比性的截图贴代码,比较一下纯XML based和Annotationed两个版本的优点和缺点。
1.New Maven Project
2.New Maven Project选择maven-archetype-webapp
3.接下来Group Id , Artifact Id ,Version
4.点Finish。接下来,maven引入以下dependencies。
spring-webmvc选择3.0.5.RELEASE版本
taglibs standard选择1.1.2版本
这里会要用到cglib,选择2.2.2版本
单元测试junit这次不会涉及到,去掉了。
至于maven如何引入,后面在maven系列中会详细介绍。
好了,最终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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.nanigac.springmvc</groupId> <artifactId>HelloWorldAnnotationed</artifactId> <packaging>war</packaging> <version>1.0.0-SNAPSHOT</version> <name>HelloWorldAnnotationed Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring-webmvc.version}</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>${taglibs.version}</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>${cglib.version}</version> </dependency> </dependencies> <build> <finalName>HelloWorldAnnotationed</finalName> </build> <properties> <spring-webmvc.version>3.0.5.RELEASE</spring-webmvc.version> <taglibs.version>1.1.2</taglibs.version> <cglib.version>2.2.2</cglib.version> </properties> </project>
5.接下来,web.xml长这样。嗯,这里也是2.5版本的。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextClass</param-name> <param-value> org.springframework.web.context.support.AnnotationConfigWebApplicationContext </param-value> </init-param> <init-param> <param-name>contextConfigLocation</param-name> <param-value>com.nanigac.springmvc.context.HelloWorldContext</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>*.htm</url-pattern> <url-pattern>/helloworld.htm</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>helloworld.htm</welcome-file> </welcome-file-list> </web-app>
大家注意XML based和Annotationed版本的<servlet></servlet>处的区别。
6.XML based版本,此处是mvc-dispatcher-servlet.xml,这里换成了com.nanigac.springmvc.context.HelloWorldContext.java,代码是这样。
package com.nanigac.springmvc.context; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping; import org.springframework.web.servlet.view.InternalResourceViewResolver; import com.nanigac.springmvc.controller.HelloWorldController; @Configuration public class HelloWorldContext { @Bean public DefaultAnnotationHandlerMapping defaultAnnotationHandlerMapping() { return new DefaultAnnotationHandlerMapping(); } @Bean public InternalResourceViewResolver internalResourceViewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/pages/"); viewResolver.setSuffix(".jsp"); return viewResolver; } @Bean public HelloWorldController HelloWorldController() { return new HelloWorldController(); } }
7.接下来,在src\main\下新建java source folder,接着在src\main\java\下创建Package:com.nanigac.springmvc.controller,并新建HelloWorldController,长这样。
package com.nanigac.springmvc.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/helloworld") public class HelloWorldController { @RequestMapping(method=RequestMethod.GET) public ModelAndView helloWorld() { ModelAndView model = new ModelAndView("HelloWorld"); model.addObject("message", "Hello World!"); return model; } }
8.接下来,移动index.asp到/WEB-INF/pages/下并rename为HelloWorld.jsp,编辑成这样。
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <body> <h1>${message}</h1> </body> </html>
9.最终Project Explorer里面项目结构长这样。
10.好吧,大功告成。选择Run AS maven clean,Run AS maven package,成功以后可以在target目录下找到HelloWorldAnnotationed.war。拷贝到%TOMCAT_HOME%/webapps/下,运行tomcat,http://localhost/HelloWorldAnnotationed/ RP没问题的话,可以看到运行成功页面写着一个大大的Hello World!。祝lucky。