为了学习spring和maven我这也是拼了老命了,光使用maven配置springmvc我花了上周一周的时间,下班回来就搞,一直有bug,一个bug接着一个,昨天一整天都在解决配置的问题,让大学同学亲自出马,这里先谢谢蒙了,今天又专门跑去新天下,找之前公司的小伙伴来帮忙。我也是参考的网上的博客,不过根据参考的自己在配置过程中也出现了好多的坑。想着使用Maven配置springmvc主要是先大致的了解下maven和spring,然后再慢慢的深入的了解maven和spring。

1.Eclipse创建Maven Project

2.选择Archetype,这里是创建web的项目,所以选择的是webapp

3.输入GroupId,Artifact Id和Viesion,这样算是输入了坐标,方便以后查找。关于坐标是maven的概念,这个以后会经常用到。

4.选中项目点右键找到属性,在build path中可以看到有几个文件不存在,可以按照缺失的增加上。

5.配置仓库

上面创建完成之后引入spring开始就bug一个接着一个,我现在pom.xml中配置了spring的几个部分,但是发现下面的错误,问了下同学告诉我说的仓库的问题,访问不了。建议使用阿里的国内的,于是我就重新设置了下仓库。需要在maven的安装目录下的conf/下的settings.xml中设置。

   <localRepository>D:\Maven\repository</localRepository>

<mirrors>节点中增加mirror.

     <mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

然后cmd中mvn help:system下载仓库jar包。其中这里要重点说一下,还有一个地方也要设置。昨天找同学搞了半天一个地方就是这个原因导致的。这里也要注意下,昨天发现这里没有配置正确,导致可以访问jsp文件,但访问Controller时就报404的错误。

6.引入springmvc

在pom.xml中引入jar包,下面的代码是正确的,但在这过程中也出现了好多坑,例如下面的错误

这个bug是因为版本不一致导致的,在web.xml中设置的是3.0的,所以需要在下图选择比较新的,但直接修改又修改不了,需要在项目的.settings文件下的org.eclipse.wst.common.project.facet.core.xml中修改。

上面的bug解决之后又出现了下面的一个bug。这个也是spring配置的问题,我在spring-mvc.xml中配置了了几个bean,因为引入的是spring5.0,所以我在每个xsd上加了个5.0.0的版本,导致报错。

下面的地方我也配置错误了,因为项目的结构在上面有,我又在classpath:加了spring/:spring-mvc.xml导致获取不到。

上面的几个不过解决之后又报404的错误,仔细检查发现是一个点应该是英文,失误写成了中文,这些可是都通过了,以为可是能跑成功了,谁知又报500的错误。查看错误信息,看着像是关于jstl的,又找同事看了下,同事让加jstl和standard,于是我就在pom.xml中增加了两个依赖,可这次tomcat都启动不起来的,一直报java.lang.IndexOutOfBoundsException的错误,这也正是我今晚又去新天下找前同事的原因,我们开始觉得是tomcat的问题,于是又下载了tomcat7,还好我的流量多,直接用手机分享热点给电脑下载,可是还是出现这样的问题,于是就排除了tomcat的问题,因为在他电脑上就是可以运行的,我昨天找了一个QQ群的网友帮我看也是可以运行的,我们就怀疑上了是不是我的eclipse的问题,想着总不能还要重新下载eclipse吧,到最后实在是不知道哪出问题了,此时我也是头大,因为这个配置搞了一星期了,哎,同事说回办公室看看在他机子上运行的,我突然觉得不会又是仓库的问题吧,因为他报的错误就是关于jstl和standard的。于是我把pom.xml的依赖删除,把仓库中的jar删除,让它重新下载,当再次启动tomcat时,奇迹发生了,没有报错,输入url没想到成功了。哈哈哈。

7.代码

1.pom.xml

<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.</modelVersion>
<groupId>com.cuiyw</groupId>
<artifactId>CywWebApp</artifactId>
<packaging>war</packaging>
<version>0.0.-SNAPSHOT</version>
<name>CywWebApp Maven Webapp</name>
<url>http://maven.apache.org</url> <!-- 定义maven变量 -->
<properties>
<!-- spring -->
<spring.version>5.0..RELEASE</spring.version>
<!-- log -->
<commons-logging.version>1.1.</commons-logging.version>
<!-- Servlet -->
<servlet.version>3.0.</servlet.version>
<jsp-api.version>2.2</jsp-api.version>
<!-- test -->
<junit.version>3.8.</junit.version>
<!-- jdk -->
<jdk.version>1.8</jdk.version>
<jstl.version>1.2</jstl.version>
<standard.version>1.1.</standard.version>
<maven.compiler.plugin.version>2.3.</maven.compiler.plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency> <dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>${standard.version}</version>
</dependency> <!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp-api.version}</version>
<scope>provided</scope>
</dependency> <!-- test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency> </dependencies>
<build>
<plugins>
<!-- define the project compile level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
</plugins>
<finalName>CywWebApp</finalName> </build>
</project>

2.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">
<display-name>test-spring</display-name> <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-content.xml</param-value>
</context-param> <!-- Spring配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- Spring MVC配置 -->
<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>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup></load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <!-- 中文过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

3.spring-mvc.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="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.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 自动扫描的包名 -->
<context:component-scan base-package="com.cyw.web.Controller" /> <!-- 默认的注解映射的支持 --> <mvc:annotation-driven> <mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean
class="org.springframework.http.converter.ResourceHttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven> <!-- 视图解释类,定义跳转的文件的前后缀 -->
<!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"
/> <property name="prefix" value="/views/" /> <property name="suffix" value=".jsp"
/> <property name="requestContextAttribute" value="rc" /> </bean> -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/view/" />
<property name="suffix" value=".jsp" />
</bean> <!-- 拦截器 -->
<!-- <mvc:interceptors> <bean class="com.fpx.ce.foundation.preference.PreferenceChangeInterceptor"
/> <mvc:interceptor> <mvc:mapping path="/page/home"/> <bean class="com.fpx.ce.foundation.user.UserInterceptor"
/> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/service/**" />
<bean class="com.fpx.ce.foundation.log.LogHandlerInterceptor" /> </mvc:interceptor>
<mvc:interceptor> <mvc:mapping path="/test/**" /> <mvc:mapping path="/service/**"
/> <bean class="com.lemon.finder.web.SharedRenderVariableInterceptor" />
</mvc:interceptor> </mvc:interceptors> --> <!-- 对静态资源文件的访问 方案一 (二选一) -->
<mvc:default-servlet-handler /> <!-- 对静态资源文件的访问 方案二 (二选一) -->
<!-- <mvc:resources mapping="/images/**" location="/images/" cache-period=""/>
<mvc:resources mapping="/js/**" location="/js/" cache-period=""/>
<mvc:resources mapping="/css/**" location="/css/" cache-period=""/> -->
</beans>

4.spring-content.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 注解注册 --> <context:annotation-config /> <context:component-scan base-package="com.cyw.web.Controller" /> </beans>

5.创建Controller,添加注解,在上面的配置文件中也有注册注解。

package com.cyw.web.Controller;

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping("/index")
public class IndexController { @RequestMapping(value="/hello.do") public ModelAndView getTest(HttpServletRequest request){
ModelAndView modelAndView = new ModelAndView("HelloWorld");
return modelAndView;
}
}

6.在webapp下创建view文件夹,然后在view下创建HelloWorld.jsp

7.整个的项目结构

最后要吐槽下,学习java真时麻烦,各种坑。

https://files.cnblogs.com/files/5ishare/WebMVC.zip

最新文章

  1. python --&gt; 正则表达式
  2. Can&#39;t update: no tracked branch
  3. php日期,时间相关知识整理
  4. Atitit selenium3 新特性
  5. 修改ViewPager调用setCurrentItem时,滑屏的速度
  6. 【BZOJ】【1529】 【POI2005】ska Piggy banks
  7. HDU3572 Task Schedule 【最大流】
  8. spring-boot配置外部静态资源的方法
  9. Swift和OC混编时, 关于@objc的作用
  10. js手写图片查看器(图片的缩放、旋转、拖拽)
  11. JavaScript+canvas 绘制多边形
  12. Xcode iOS布局autolayout和sizeclass的使用
  13. 用user-selection实现让页面上的内容不能被选中
  14. 机器学习——交叉验证,GridSearchCV,岭回归
  15. HTML 5 &amp; checkbox &amp; switch components
  16. MySQL前缀索引和索引选择性
  17. 通过第三方软件打开sqlite
  18. Hadoop系列之(二):Hadoop集群部署
  19. 使用 MVVMLight 命令绑定(转)
  20. Python模块NumPy中的tile(A,rep) 函数

热门文章

  1. Python爬虫总结
  2. 《MySQL必知必会》[02] 多表联合查询
  3. C# 下搭建最新版OpenCV(Emgu CV)开发环境
  4. hadoop2.0的数据副本存放策略
  5. scoke摘要
  6. Opengl4.5 中文手册—A
  7. .NET Excel导出方法及其常见问题详解
  8. Angularjs –– Expressions(表达式)
  9. JAVA设计模式总结之六大设计原则
  10. java异步线程池同时请求多个接口数据