1 首先在eclipse用maven,创建简单的web工程
2 修改pom.xml 添加
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.4.RELEASE</version> </dependency>
3 修改web.xml (默认是servlet 2.3的不支持el 改成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"> <!-- spring 入口配置 --> <servlet> <servlet-name>springmvc</servlet-name> <!-- 扩展SpringMvc DispatcherServlet,启动绑定initParam参数 --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- springmvc配置文件路径 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- springMvc 请求后缀 --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name > <url-pattern>/*</url-pattern> </filter-mapping> <display-name>Archetype Created Web Application</display-name> </web-app>
4 在 src/main/resources 资源目录下添加 第三步配置的springmvc-servlet.xml文件
<mvc:annotation-driven/>表示使用注解进行开
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <description>Spring mvc配置</description> <mvc:annotation-driven/> <context:component-scan base-package="com.keeley.Controller"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 定义首页 --> <mvc:view-controller path="/" view-name="redirect:/test" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
5 在src/main 下面添加目录 java 然后maven:clean下,eclipse就能识别成source目录了
然后建立Controller
package com.keeley.Controller; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class TestController { @RequestMapping("/test") public String test(String name,Map<String,Object> out){ //name 传递到这个服务的参数 //Map传递到前端作用域的MAP 前端用${key}获取值 if(name==null) name=""; out.put("msg", "欢迎你:"+name); return "test"; } }
6 在webapp下面新建目录 jsp(前面spring配置的统一输出路径),jsp目录新建test.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>springmvc</title> </head> <body> ${msg }<br/> </body> </html>
7 运行 maven clean tomcat:run