spring mvc入门
Spring相关的jar文件下载:http://repo.spring.io/libs-release/org/springframework/spring/
commons-logging-1.2.jar http://commons.apache.org/proper/commons-logging/download_logging.cgi
需要导入的jar包列表 (spring jar包版本和servletName-servlet.xml中的xsi:schemaLocation属性最好一致,否则可能报错)
commons-logging-1.2.jar spring-aop-4.2.0.RELEASE.jar spring-aspects-4.2.0.RELEASE.jar spring-beans-4.2.0.RELEASE.jar spring-context-4.2.0.RELEASE.jar spring-core-4.2.0.RELEASE.jar spring-expression-4.2.0.RELEASE.jar spring-web-4.2.0.RELEASE.jar spring-webmvc-4.2.0.RELEASE.jar
建立一个HiSpring动态Web项目
将以上jar包加入到HiSpring\WebContent\WEB-INF\lib目录
java build path=》Libraries=》Add JARs
如果jar包没放在WebContent\WEB-INF\lib目录目录下,则在运行时可能会报错:无法找到org.springframework.web.servlet.DispatcherServlet
控制台报以下错误:java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>springwebtest</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
package com.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; public class HelloController extends AbstractController { @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { String value = request.getParameter("hello"); System.out.println("------value:" + value); //返回数据和页面 ModelAndView view = new ModelAndView("index"); view.addObject("helloworld", "hello" + value); return view; } }
在WebContent\WEB-INF目录下对每个servlet创建一个[servlet-name]-servlet.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" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> <!-- 配置handler mapping, 根据bean name找到对应的controller 可以省略 --> <bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"></bean> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="view/"></property> <!-- 后缀 --> <property name="suffix" value=".jsp"></property> </bean> <!-- 配置controller --> <bean name="/hello.do" class="com.controller.HelloController"></bean> </beans>
WebContent\hello.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> <form action="hello.do" method="post"> hello:<input type="text" name="hello"></input><input type="submit" value="提交"> </form> </body> </html>
WebContent\view\index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="ISO-8859-1"%> <!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> <p>${helloworld}</p> </body> </html>
访问地址
http://127.0.0.1:8080/HiSpring/hello.jsp
==================================================================
HandlerMapping的本质就是找到Controller
可以有多种不同的映射方式:
ControllerBeanNameHandlerMapping 根据beanname来配置
SimpleUrlHandlerMapping 把一个URL映射到Controller
ControllerClassNameHandlerMapping 根据控制类的类名访问controller
SimpleUrlHandlerMapping
<?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" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> <bean id="helloController" class="com.controller.HelloController" /> <bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <!--方法一 --> <!--property name="urlMap"> <map> <entry key="/hello.do" value-ref="helloController" /> </map> </property--> <!--方法二 --> <property name="mappings"> <props> <prop key="/hello.do">helloController</prop> </props> </property> <!-- 方法三 此时urlMap.properties文件应放在WebRoot目录下 --> <!--property name="mappings"> <bean class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location"> <value>urlMap.properties</value> </property> </bean> </property--> </bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="view/"></property> <!-- 后缀 --> <property name="suffix" value=".jsp"></property> </bean> </beans>
ControllerClassNameHandlerMapping
<?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" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="view/"></property> <!-- 后缀 --> <property name="suffix" value=".jsp"></property> </bean> <bean class="com.controller.HelloController"/> </beans>

浙公网安备 33010602011771号