初始springMvc這個框架,非常的陌生,而且幸好公司是通過maven這個代碼管理工具,可以隨時添加依賴。解決了很多問題在以後深入開發中。 項目結構: 通過結構中,pom.xml這個文件其實就說明這個項目是通過maven構建的,pom.xml里是主要負責構建jar或者war的依賴。其代碼如下: 從 ...
初始springMvc這個框架,非常的陌生,而且幸好公司是通過maven這個代碼管理工具,可以隨時添加依賴。解決了很多問題在以後深入開發中。
項目結構:
通過結構中,pom.xml這個文件其實就說明這個項目是通過maven構建的,pom.xml里是主要負責構建jar或者war的依賴。其代碼如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>taofuxn_web</groupId> <artifactId>springMvc</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springMvc Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!-- spring需要的jar包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.4.RELEASE</version> <type>jar</type> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2.4.RELEASE</version> <type>jar</type> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>3.2.4.RELEASE</version> <type>jar</type> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.4.RELEASE</version> <type>jar</type> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>3.2.4.RELEASE</version> <type>jar</type> </dependency> </dependencies> <build> <finalName>springMvc</finalName> </build> </project>
從中可以看出,如果要設置依賴jar,那麼就要知道jar的相關信息。如何得知呢....
尋師問友後,得到了一個站點http://mvnrepository.com/,搜索你要的那個依賴,裡面有相應的版本。
關於springMvc和maven的配置就上面的pom.xml,當然還可以配置Mybxxxs,hibernate..
接下來就要應用springmvc 的相關配置了:
首先配置的是 項目中的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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>springmvc</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 可以自定義servlet.xml配置文件的位置和名稱,預設為WEB-INF目錄下,名稱為[<servlet-name>]-servlet.xml,如spring-servlet.xml--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <!-- mvc-攔截所有的請求 --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
根據上面的註釋我們知道要再相應的目錄下手動的新建一個spring-servlet.xml文件進行spring的配置信息
其次spring-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" 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"> <!-- 激活註解的的標簽 --> <context:annotation-config/> <!-- 只搜索有controller註解的類,同時這個包名別搞錯了,不然就找不到controller --> <context:component-scan base-package="cn.taofu" > <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsps/" /><!-- 這個是配置在當下目錄尋找jsp文件--> <property name="suffix" value=".jsp" /> </bean> </beans>
配置好了web.xml和spring-servlet.xml,基本的spring的運行環境就有了,接下來就要生成一個測試controller
package cn.taofu; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/test") public class TestController { @RequestMapping("/mvc") public String getMode(){ System.out.println("hello springmvc"); return "index"; } }
好吧,這是最簡單的應用了,只要在相應的目錄下生成index.jsp文件。當請求訪問該方法上的mvc連接時就會跳轉到index.jsp,訪問的鏈接如:localhost:8080/springMvc/test/mvc ->回車。基本就能調通這個前後
代碼雖然入門初級,運行起來了我也是掉了塊肉了。當然其中的問題也不少。
問題一、在啟動伺服器出現的報錯信息
java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523) at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:525) at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:507) at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:126) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1099) at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1043) at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4957) at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5284) at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5279) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) at java.util.concurrent.FutureTask.run(FutureTask.java:138) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:662)
這種錯誤很鬱悶,單憑初級的開發是很難確定是什麼問題,所有隻有去到頂級程式員的家棧溢出,發現如下解決辦法:
這個方法標註了,只有maven構建的應用,如不是還是多去google吧
You need to add the "Maven Dependency" in the Deployment Assembly - right click on your project and choose properties. - click on Deployment Assembly. - click add - click on "Java Build Path Entries" - select Maven Dependencies" - click Finish. Rebuild and deploy again Note: This is also applicable for *non maven* project.
問題二、少了兩個jar,
message Handler processing failed; nested exception is java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config description The server encountered an internal error that prevented it from fulfilling this request. exception org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config org.springframework.web.servlet.DispatcherServlet.triggerAfterCompletionWithError(DispatcherServlet.java:1259) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827) javax.servlet.http.HttpServlet.service(HttpServlet.java:622) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) root cause java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config org.springframework.web.servlet.support.JstlUtils.exposeLocalizationContext(JstlUtils.java:97) org.springframework.web.servlet.view.JstlView.exposeHelpers(JstlView.java:135) org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:211) org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:263) org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208) org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827) javax.servlet.http.HttpServlet.service(HttpServlet.java:622) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
這種錯誤比較好找,百度一下發現需要兩個jar.於是導入如下文件解決
過程是很艱難的,但結果還是可以的。努力