<?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"> <!-- 定义springmvc的前端控制器 --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- Spring mvc下的配置文件的位置和名称 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 让控制器拦截所有的请求 --> <servlet-mapping> <servlet-name>springDispatcherServlet</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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 配置handler,映射/hello请求 --> <bean name="/hello" class="com.game.controller.HelloController"></bean> <!-- 处理映射器将bean的name作为url进行查找,需要在配置handler时指定name(即url)--> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- 所有适配处理器都要实现HandlerAdapter接口 --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean> <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean> </beans>
package com.game.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class HelloController implements Controller { private static final Log logger = LogFactory.getLog(HelloController.class); /** * handleRequest是controller接口必须实现的方法 * 该方法的参数对应请求的request和response * 必须返回一个包含一个视图名或试图名和模型的ModelAndView对象 * */ @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub logger.info("handleRequest被调用"); ModelAndView mv = new ModelAndView(); //添加模型数据,可以是任意的pojo对象 mv.addObject("message", "Hello World"); //设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面 mv.setViewName("/WEB-INF/views/welcome.jsp"); return mv; } }
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>welcome</title> </head> <body> <!-- 页面可以访问到controller传递出来的message --> ${ requestScope.message } </body> </html>
http://localhost:8080/SpringMVCTest/hello
就能在页面上看到Hello World了
当然需要把相关的jar包放在WEB-INF/lib目录下
代码中都有相关注释,慢慢理解吧,我也是初学,留个笔记,方便自己,方便他人。
原文:http://www.cnblogs.com/tengfeixinxing/p/6965463.html