1. 在eclipse下建maven工程:
在JEE版的eclipse下, New-->Maven Project-->到选Archetype,选择maven-archetype-webapp, Next-->输入GroupId, ArtifactId等,然后完成.
?
2. 在生成的pom.xml加入:
class="java"><dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.0.7.RELEASE</version> </dependency>
3. 在工程根目录运行: mvn eclipse:eclipse? 生成项目文件
4. web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="WebApp_ID" version="2.4" xsi:schemaLocation=" http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
?5. spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename"> <value>messages</value> </property> </bean> <context:component-scan base-package="test1" /> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> </beans>
?6. 简单的controller
package test1; import java.util.Arrays; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/main.do") public class WebController { @RequestMapping public String load(ModelMap modelMap) { List<String> list = Arrays.asList("xxxx", "yyyy"); modelMap.put("list", list); modelMap.put("test2", "Hello Test 2"); return "main"; } }
?7. main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <html> <body> <h2>Hello World!</h2> <h3> <spring:message code="test1" /> </h3> <h3> ${list} ${test2} </h3> </body> </html>
8. 资源文件messages.properties : test1=Hello Test
?
9. 运行tomcat:
set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,address=4044,server=y,suspend=n -Xms128m -Xmx512m mvn clean tomcat:run -DskipTests -Dmaven.tomcat.port=8080
?
?