<?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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SpringMVC1</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <!-- 同struts一样,也是需要拦截请求 --> <servlet> <servlet-name>springmvc</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> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern><!-- 拦截所有以.do结尾的请求 --> </servlet-mapping> </web-app>
spring-mvc.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 使用注解的包,包括子集 --> <context:component-scan base-package="com.maya"/> <!-- 视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/hello/" /><!-- 返回视图到这个目录下 --> <property name="suffix" value=".jsp"></property> </bean> </beans>
写一个控制层
package com.maya.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller//注解 public class HelloWorld { @RequestMapping("/hello") public String helloWorld(Model model){ model.addAttribute("messager","SpringMVC你好!"); return "helloWorld"; } } /* *用户发送请求首先被org.springframework.web.servlet.DispatcherServlet拦截后分配请求找到相应的方法 *当请求是hellow.do的时候,根据注解@RequestMapping后的(内容)找到对应的方法,然后返回"helloWorld"到spring-mvc.xml的视图层 *,然后回到你设定的目录下找 返回值.jsp的文件 */
第一个springMVC下的hellowWorld就完成了
原文:http://www.cnblogs.com/AnswerTheQuestion/p/6680538.html