你的位置:首页 > Java教程

[Java教程]java之Maven配置和springMvc的简单应用


初始springMvc这个框架,非常的陌生,而且幸好公司是通过maven这个代码管理工具,可以随时添加依赖。解决了很多问题在以后深入开发中。

项目结构:

通过结构中,pom.

<project ="http://maven.apache.org/POM/4.0.0" ="http://www.w3.org/2001/  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.


接下来就要应用springmvc 的相关配置了:

首先配置的是 项目中的web.

<??><web-app ="http://www.w3.org/2001/ ="http://java.sun.com/ xsi:schemaLocation="http://java.sun.com/ 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.-->       <init-param>        <param-name>contextConfigLocation</param-name>        <param-value>/WEB-INF/spring-servlet.</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.

其次spring-servlet.

<??><beans ="http://www.springframework.org/schema/beans"  ="http://www.w3.org/2001/ ="http://www.springframework.org/schema/p"  ="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.

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 againNote: This is also applicable for *non maven* project.

 

问题二、少了两个jar,

message Handler processing failed; nested exception is java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Configdescription The server encountered an internal error that prevented it from fulfilling this request.exceptionorg.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 causejava.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.于是导入如下文件解决

 

 


 

过程是很艰难的,但结果还是可以的。努力