c目錄結構:
contents structure [-]
MVC的全稱是Model View Controller,通過實現MVC框架可以很好的數據、視圖、業務邏輯進行分離。Spring MVC也是一種MVC框架,它是SpringFrameWork的後續產品,同樣需要Spring的Jar包才能運行起來。
Spring MVC的設計是圍繞著DispatcherServlet展開的,由DispatcherServlet將請求派發到特定的Controller。
下面是SpringMVC的一個簡單流程圖:
SpringMVC入門示例
我的項目結構:
1,復制Jar包
將Spring的Jar包復制到lib目錄下。
配置Web.xml文件
<?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_3_0.xsd" id="WebApp_ID" version="3.0"> <!-- this name matched project name --> <display-name>SpringMVC</display-name> <!-- transmit to dispacherServlet to deal with information that passed from table servlet-mapping --> <servlet> <servlet-name>mySpring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>mySpring</servlet-name> <url-pattern>*.ms</url-pattern> </servlet-mapping> <!-- solve messy code --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
在工程的WEB-INF下面創建dispatcherServlet配置文件[servlet-name]-servlet.xml。
文件名的格式必需為“[servlet-name]-servlet.xml”,比如筆者的web文件裡servlet-name為mySpring,則文件名必須為mySpring-servlet.xml,如果放錯的位置或是文件名稱格式不對,那麼通過web.xml文件是找不到dispatcherServlet文件的。也可以說,只要在Web.xml裡配置好了DispatcherServlet,那麼該DispatcherServlet的名稱也就唯一確定了,一個web.xml文件可以映射出多個DispatcherServlet文件。
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd 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.3.xsd"> <!-- configure HandlerMapping --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- configure HandlerAdapter --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean> <!-- configure controller --> <bean name="/testSpring.ms" class="com.my.spring.TestSpring"></bean> <!-- configure viewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass"> <value>org.springframework.web.servlet.view.InternalResourceView</value> </property> <property name="prefix" value="/view/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
<%@ 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="testSpring.ms"> input your words: <input type="text" name="words"/> <input type="submit" value="提交"> </form> </body> </html>
<%@ 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> ${myValue} </body> </html>
package com.my.spring; import java.util.HashMap; import java.util.Map; 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 TestSpring extends AbstractController { @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub String value=request.getParameter("words"); //create model Map<String,String>model=new HashMap<String,String>(); //set value model.put("myValue", value); //create modelAndView ModelAndView mav=new ModelAndView("result",model); return mav; } }
這個文件必需要實現AbstrcactController抽象類,或是實現Controller接口。然後重寫handlerRequestInternal方法。這裡一定要選擇servlet下的AbstractController或Controller,如果讀者選擇了portlet,恰好項目下又沒有相應的包,那麼就會報錯。
錯誤代碼:
The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
錯誤解決:
這是因為項目下沒有該類,添加servlet-api.jar即可,詳見Eclipse如何發布Web項目。
錯誤代碼:
Server Tomcat v8.5 Server at localhost failed to start.
錯誤解決:
造成這個錯誤的原因有許多,這裡建議讀者檢查一下自己的Web.xml文件中的語法是否正確。
錯誤代碼:
Multiple markers at this line
- The type javax.portlet.ActionResponse cannot be resolved. It is indirectly
referenced from required .class files
- The type javax.portlet.RenderRequest cannot be resolved. It is indirectly
referenced from required .class files
- The type javax.portlet.RenderResponse cannot be resolved. It is indirectly
referenced from required .class files
- The type javax.portlet.ActionRequest cannot be resolved. It is indirectly
referenced from required .class files
- The type javax.portlet.PortletContext cannot be resolved. It is indirectly referenced
from required .class files
問題解決:
這是因為讀者的項目下面沒有,portlet包導致的,portlet包的下載。
除了引入相應的Portlet包外,這裡讀者也可以將
import org.springframework.web.portlet.ModelAndView; import org.springframework.web.portlet.mvc.AbstractController;
改為:
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController;
如果報出的錯誤是JSTL標簽的原因,那麼下載一個標簽標准文檔庫即可,下載頁面。
本博文是博主原創文章,如需轉載請注明出處。