springmvc快速入門(XML版本)
阿新 • • 發佈:2018-12-04
1)springmvc快速入門(傳統版)
步一:建立springmvc-day01這麼一個web應用
步二:匯入springioc,springweb , springmvc相關的jar包
步三:在/WEB-INF/下建立web.xml檔案
<servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping>
步四:建立HelloAction.java控制器類
/** * 控制器 */ public class HelloAction implementsController{ /** * 業務方法 */ public ModelAndView handleRequest(HttpServletRequest requqest,HttpServletResponse response) throws Exception { ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("message","這是我的第一個springmvc應用程式"); modelAndView.setViewName("/jsp/success.jsp"); return modelAndView; } }
步五:在/WebRoot/下建立jsp/success.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>這是我的第一個springmvc應用程式</title> </head> <body> success.jsp<br/> ${message} </body> </html>
步六:在/WEB-INF/建立DispatcherServlet-servlet.xml配置檔案,xml頭部資訊與spring.xml相同
注意:該配置檔案的命名規則:web.xml檔案中配置的<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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <!-- 控制器(程式設計師) --> <bean name="/hello.action" class="loaderman.javaee.springmvc.base.HelloAction"></bean> <!-- 對映器(框架) --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- 介面卡(框架) --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean> <!-- 檢視解析器(框架) --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean> </beans>
步七:部署web應用到tomcat中,通過瀏覽器訪問helloaction的URL