springMVC入門案例
阿新 • • 發佈:2018-09-05
src 目錄 content color adapter frame 屬性 model add
1、配置文件的web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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"> 3 <display-name>springmvc1</display-name> 4 5 <servlet> 6 <servlet-name>springmvc</servlet-name> 7 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 8 <!-- 默認加載方式必須規範: 9 * 文件命名:servlet-name-servlet.xml====springmvc-servlet.xml10 * 路徑規範:必須在WEB-INF目錄下面 --> 11 <init-param> 12 <param-name>contextConfigLocation</param-name> 13 <param-value>classpath:springmvc.xml</param-value> 14 </init-param> 15 </servlet> 16 17 <servlet-mapping> 18 <servlet-name>springmvc</servlet-name> 19 <url-pattern>*.do</url-pattern> 20 </servlet-mapping> 21 22 <welcome-file-list> 23 <welcome-file>index.html</welcome-file> 24 <welcome-file>index.htm</welcome-file> 25 <welcome-file>index.jsp</welcome-file> 26 <welcome-file>default.html</welcome-file> 27 <welcome-file>default.htm</welcome-file> 28 <welcome-file>default.jsp</welcome-file> 29 </welcome-file-list> 30 </web-app>
2、springMVC.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xmlns:tx="http://www.springframework.org/schema/tx" 8 xsi:schemaLocation="http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 12 http://www.springframework.org/schema/context 13 http://www.springframework.org/schema/context/spring-context-3.2.xsd 14 http://www.springframework.org/schema/aop 15 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 16 http://www.springframework.org/schema/tx 17 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> 18 19 <!-- 配置處理器映射器,springmvc默認的處理器映射器 20 BeanNameUrlHandlerMapping:根據bean(自定義Controler)的name屬性的url去尋找hanler(Action:Controller) --> 21 <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> 22 23 <!-- 配置處理器適配器執行Controlelr ,springmvc默認的SimpleControllerHandlerAdapter:執行Controller --> 24 <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> 25 26 <!-- 配置自定義Controler --> 27 <bean id="myController" name="/hello.do" class="com.xiaostudy.MyController"/> 28 29 <!-- 配置sprigmvc視圖解析器:解析邏輯試圖 30 後臺返回邏輯試圖:index 31 視圖解析器解析出真正物理視圖:前綴+邏輯試圖+後綴====/WEB-INF/index.jsp --> 32 <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 33 <property name="prefix" value="/WEB-INF/"/> 34 <property name="suffix" value=".jsp"/> 35 </bean> --> 36 </beans>
3、Controller.java
1 package com.xiaostudy; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.springframework.web.servlet.ModelAndView; 7 import org.springframework.web.servlet.mvc.Controller; 8 9 public class MyController implements Controller{ 10 11 public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { 12 //創建視圖 13 ModelAndView mv = new ModelAndView(); 14 15 //設置頁面回顯數據 16 mv.addObject("hello", "xiaostudy"); 17 18 //返回物理視圖,指定跳轉的視圖 19 // mv.setViewName("/WEB-INF/index.jsp"); 20 21 //返回邏輯視圖 22 mv.setViewName("index"); 23 24 return mv; 25 } 26 27 }
4、index.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 3 "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>springMVC_demo</title> 8 </head> 9 <body> 10 ${hello } 11 </body> 12 </html>
5、所需jar包
6、項目文件位置
springMVC入門案例