【转载请注明出处:http://blog.csdn.net/leytton/article/details/38514115】
参考资料 http://blog.csdn.net/leytton/article/details/38502665
下载 http://pan.baidu.com/s/1jGEAZ9G
spring-aop-3.2.0.RELEASE.jar spring-aspects-3.2.0.RELEASE.jar spring-beans-3.2.0.RELEASE.jar spring-context-3.2.0.RELEASE.jar spring-context-support-3.2.0.RELEASE.jar spring-core-3.2.0.RELEASE.jar spring-expression-3.2.0.RELEASE.jar spring-instrument-3.2.0.RELEASE.jar spring-instrument-tomcat-3.2.0.RELEASE.jar spring-jdbc-3.2.0.RELEASE.jar spring-jms-3.2.0.RELEASE.jar spring-orm-3.2.0.RELEASE.jar spring-oxm-3.2.0.RELEASE.jar spring-test-3.2.0.RELEASE.jar spring-tx-3.2.0.RELEASE.jar spring-web-3.2.0.RELEASE.jar spring-webmvc-3.2.0.RELEASE.jar spring-webmvc-portlet-3.2.0.RELEASE.jar
commons-logging.jar //注:日志jar包
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list> <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*:com/llqqww/config/springMVC-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <!--启动顺序,tomcat一启动就初始化SprinMVC--> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>注:
init-param 中设置其他配置文件路径,没有init-param默认是在/WEB-INF目录下所有*-servlet.xml文件
本文配置文件springMVC-servlet.xml在src的com.llqqww.config包内
spring配置文件名需与web.xml中的<servlet-name></servlet-name>中一致
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <bean name="/hi/helloworld" class="com.llqqww.controller.HelloWorldController" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>注:
/hi/helloworld是访问Controller的URL路径、class是该URL访问的Controller
prefix 若是'/',则是用户可直接访问WebRoot下的jsp文件、若是"/WEB-INF/jsp/",则只能通过Controller映射访问jsp文件(不能直接访问"/WEB-INF"目录下文件)
新建com.llqqww.controller包,创建类HelloWorldController 继承Controller 类,实现handleRequest方法
package com.llqqww.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class HelloWorldController implements Controller { public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { System.out.println("-------Welocome-----"); return new ModelAndView("/welcome"); } }
welcome.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> <body> Welcome~ </body> </html>
启动tomcat部署项目,浏览器访问如:localhost:8080/hi/helloworld 则会看到welcome.jsp文件中的"Welcome~"信息