每次需要Spring MVC的web项目测试一些东西时,都苦于手头上没有最简单的Spring MVC的web项目,现写一个。
> 版本说明
首先要引入一些包,Spring的IOC、MVC包就不用说了。还有Jackson的包,因为默认情况下Controller的方法如要返回Json,用的是此框架。
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-webflow</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
<!-- 要引入,否则请求返回JSON的接口会报“406–Not Acceptable” -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
</dependencies>
> 搭建一个最简单的Spring MVC项目
在web.xml中注册Spring的分发器,同时说明了分发器依据的配置文件(在此是spring-mvc.xml)在哪里:
<?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" 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"> <display-name>002 Spring Application</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>Spring MVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> <!-- <param-value>/WEB-INF/spring-mvc.xml</param-value> --> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Spring MVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
那么配置文件该怎么样配置呢:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!--扫描注解 --> <context:component-scan base-package="com.nicchagil" /> <mvc:annotation-driven/> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </list> </property> </bean> </beans>
这个文件告诉Spring:
1、要在“com.nicchagil”包下扫描注解的组件,比如“@Controller”等等。
2、启动默认的Spring MVC注解,比如“@ResponseBody”等。
3、返回的视图(页面)的呈现方式,这里会拼接前缀“/jsp”和后缀“.jsp”以定位页面的路径。
写一个简单的Controller:
package com.nicchagil.controller; import java.util.Map; import java.util.TreeMap; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/user") public class UserController { @RequestMapping("/query") public String query() { // http://localhost:8080/003SpringWeb/user/query System.out.println("query trgger."); return "user/userList"; } @RequestMapping("/queryForApp") @ResponseBody public Object queryForApp() { // http://localhost:8080/003SpringWeb/user/queryForApp System.out.println("queryForApp trgger."); Map<String, Object> map = new TreeMap<String, Object>(); map.put("userName", "Nick Huang"); return map; } }
再写一个页面:
这个页面放在哪里?根据query()方法返回的“user/userList”,加上配置文件配置的前缀“/jsp”和后缀“.jsp”,我们知道应该放在“/jsp/user/userList.jsp”。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>User List</title> </head> <body> This is user list demo page. </body> </html>
启动项目后,如果你绑定的是8080端口,那么:
1、用浏览器访问“http://localhost:8080/003SpringWeb/user/query”,你可以看到页面。
2、用浏览器访问“http://localhost:8080/003SpringWeb/user/queryForApp”,你可以看到以下的Json:
{"userName":"Nick Huang"}
代码在(如果所在地址已调整,请周围找找=_=!):https://github.com/nicchagil/some-exercise/tree/master/study-and-demo/Spirng-MVC-Exercise