??? 借助Eclipse4.3 + Maven3.0.3构建Java Web应用程序。使用Maven内置的servlet 容器jetty,不需手工集成Web服务器到Eclipse。还帮我们自动创建Java EE规范定义的Java Web应用程序的有效目录结构,如:WEB-INF/classes目录等,方便省心。当然Maven还有其他优秀的功能,这里不会详细介绍。pom.xml的内容可见第6小节。
??? 接下来将介绍如何使用Spring MVC开发简单的Web应用程序,就从创建配置文件开始。
??? 在web.xml中,定义一个类型为DispatcherServlet的Servlet。这是Spring MVC的核心Servlet类,它负责接受Web请求,并将它们分发到相应的处理程序中去。代码如下:
<servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 初始化参数 :确定spring-servlet.xml文件路径 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 拦截所有以 /mvc/开始的请求 --> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/mvc/*</url-pattern> </servlet-mapping>
??? Spring-servlet.xml是一个标准的Spring Bean配置文件,内容如上一节。
?? ? 代码如下:
?
package org.mahz.springmvc; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class IndexAction{ private String username; private String viewPage; @RequestMapping("/index") public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { Map<String, String> model = new HashMap<String, String>(); model.put("username", getUsername()); return new ModelAndView(getViewPage(), model); } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getViewPage() { return viewPage; } public void setViewPage(String viewPage) { this.viewPage = viewPage; } }
?
?????? 在handleRequest()方法中,可以随意地处理Web请求,就像在Servlet中一样。最后返回一个ModelAndView对象,其中包含视图名称或者视图对象,以及一些模型属性。
? ? 创建好控制器类后,必须在Web应用程序上下文中声明它的实例。由于这是表现层组件,所以应该在spring-servlet.xml中声明它。代码如下:
?
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="org.mahz.springmvc" /> <!-- 一般视图配置 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/jsp/" p:suffix=".jsp" /> <!-- 定义控制器 --> <bean id="indexAction" class="org.mahz.springmvc.IndexAction"> <property name="username"> <value>刘小s</value> </property> <property name="viewPage"> <value>index</value> </property> </bean> </beans>
?
??? 在webapp目录下创建jsp目录,在jsp目录中添加index.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> <% String str = (String)request.getAttribute("username"); %> <body> <H1><%=str%>,欢迎你!</H1> </body> </html>
??? 在Maven使用jetty:run部署Web应用程序。
??? 在浏览器中输入链接:http://localhost:8080/springmvc/mvc/index
??? pom.xml文件中,包括项目的基本系统、Spring MVC所需要的依赖信息、项目的编译插件和jetty插件信息。内容如下:
<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>org.huazhen</groupId> <artifactId>springmvc</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>springmvc</name> <!-- 属性配置 --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <junit.version>4.10</junit.version> <spring.version>2.5.6</spring.version> </properties> <!-- 依赖配置 --> <dependencies> <!-- servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- Freemarker --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.18</version> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> </dependencies> <build> <finalName>springmvc</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.26</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <stopKey>foo</stopKey> <stopPort>9999</stopPort> </configuration> <executions> <execution> <id>start-jetty</id> <phase>pre-integration-test</phase> <goals> <goal>run</goal> </goals> <configuration> <scanIntervalSeconds>0</scanIntervalSeconds> <daemon>true</daemon> </configuration> </execution> <execution> <id>stop-jetty</id> <phase>post-integration-test</phase> <goals> <goal>stop</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
?