springmvc环境搭建以及常见问题解决
1.新建maven工程
a)? 打開(kāi)eclipse,file->new->project->Maven->Maven Project
?b)? 下一步
c) ? 選擇創(chuàng)建的工程為webapp,下一步
?d) ? 填寫(xiě)項(xiàng)目的group id和artifact id。一般情況下,group id寫(xiě)域名的倒序,artifact id寫(xiě)項(xiàng)目名稱即可。最后點(diǎn)完成。
e)?? 最初建好后,項(xiàng)目目錄結(jié)構(gòu)如下
f)?? 一般的項(xiàng)目目錄中,還有src/main/java,src/main/test/java,src/main/test/resources這 三個(gè)source folder,需要手動(dòng)創(chuàng)建。
?
2. 修改項(xiàng)目基本設(shè)置
a)??? 右鍵此項(xiàng)目名稱->Properties->Java Build path,點(diǎn)擊source標(biāo)簽。
?
?b)? 將上missing的文件夾刪除,然后重新添加,如下:
?c)?? 重新添加之后的效果如下:
d)?? 如果某些folder不想 build path,直接remove就行了(本人只選擇了src/main/java, 和 src/main/resources),最終如下:
?e)? 修改jre系統(tǒng)
?
?f)? 修改java compiler compliance level 與 jre系統(tǒng)的level一致
?g) 修改Project Facets
Dynamic Web Module無(wú)法在這里直接修改為3.0,需要打開(kāi)工程目錄下有一個(gè).settings文件夾,打開(kāi)org.eclipse.wst.common.project.facet.core.xml,做如下修改:
<installed facet="jst.web" version="3.0"/>重啟eclipe就可以看到更改生效了。
3.必要的配置文件
?在Java Resources/scr/main/resources目錄下,創(chuàng)建configs文件夾,以便存放在web.xml中聲明的配置路徑
applicationContext.xml (Spring的公共配置文件)
<?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:p="http://www.springframework.org/schema/p"xmlns:cache="http://www.springframework.org/schema/cache" xmlns:repo="http://www.springframework.org/schema/data/repository"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.7.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd "default-lazy-init="true"><description>Spring公共配置</description><!-- 使用annotation 自動(dòng)注冊(cè)bean, 并保證@Required、@Autowired的屬性被注入 --><context:component-scan base-package="com.ds"><context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller" /><context:exclude-filter type="annotation"expression="org.springframework.web.bind.annotation.ControllerAdvice" /></context:component-scan></beans> View Codespring-mvc-config.xml (springmvc的配置文件)
<?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:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><context:component-scan base-package="com.ds"use-default-filters="false"><context:include-filter type="annotation"expression="org.springframework.stereotype.Controller" /><context:include-filter type="annotation"expression="org.springframework.web.bind.annotation.ControllerAdvice" /></context:component-scan><!-- json Converter配置 --> <mvc:annotation-driven><mvc:message-converters register-defaults="true"><bean class="org.springframework.http.converter.StringHttpMessageConverter"><constructor-arg value="UTF-8" /></bean><beanclass="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"><property name="prettyPrint" value="true" /></bean></mvc:message-converters></mvc:annotation-driven><bean id="freemarkerConfigurer"class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"><property name="templateLoaderPath" value="/WEB-INF/views/" /><property name="defaultEncoding" value="UTF-8" /><property name="freemarkerSettings"><props><prop key="template_update_delay">10</prop><prop key="locale">zh_CN</prop><prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop><prop key="date_format">yyyy-MM-dd</prop><prop key="defaultEncoding">utf-8</prop></props></property><property name="freemarkerVariables"><map><entry key="ctx" value="/demo" /></map></property></bean> <bean id="viewResolver"class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"><property name="viewClass"value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" /><property name="suffix" value=".ftl" /><property name="contentType" value="text/html;charset=UTF-8" /><property name="exposeRequestAttributes" value="true" /><property name="exposeSessionAttributes" value="true" /><property name="exposeSpringMacroHelpers" value="true" /><property name="requestContextAttribute" value="request" /><!-- 多ViewResovler配置 ,值越小就優(yōu)先解析,這里的配置是 先找 ftl,再去找 mv ,最后去找jsp文件 --><property name="order" value="1" /><property name="cache" value="true" /></bean><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/" /><property name="suffix" value=".jsp" /><property name="order" value="2" /></bean><mvc:default-servlet-handler /></beans> View Code?
web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name>Archetype Created Web Application</display-name><description>sprintMVC環(huán)境搭建</description><!-- 加載Spring配置文件 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:/config/applicationContext.xml</param-value></context-param><!-- Spring監(jiān)聽(tīng) --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- Spring MVC配置 --><servlet><servlet-name>Dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 自定義spring mvc的配置文件名稱和路徑 --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:config/spring-mvc-config.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><!-- spring mvc 請(qǐng)求后綴 --><servlet-mapping><servlet-name>Dispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list> </web-app> View Code?
最后添加工程依賴的jar包,也就是配置pom.xml, 最終效果圖如下
注:其實(shí)測(cè)試的話沒(méi)有必要依賴這么多了,只需要將spring-webmvc依賴上就行了。
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.0</modelVersion><groupId>com.springmvc</groupId><artifactId>test</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>dsdemo Maven Webapp</name><url>http://maven.apache.org</url><profiles><profile><id>jdk-1.7</id><!-- 另外一種激活方式 --><activation><activeByDefault>true</activeByDefault><jdk>1.7</jdk></activation><properties><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target> <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion></properties></profile></profiles><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency><groupId>io.springside</groupId><artifactId>springside-core</artifactId><version>4.3.0-RELEASE</version><classifier>RELEASE</classifier></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.2.5.RELEASE</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version></dependency><dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.20</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.1.9.RELEASE</version></dependency><!-- jackson api --><dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.1.0</version> </dependency><dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><version>2.4</version><classifier>jdk15</classifier></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.35</version></dependency><!-- hibernate begin --><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-entitymanager</artifactId><version>4.3.5.Final</version></dependency><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-ehcache</artifactId><version>4.3.5.Final</version><exclusions><exclusion><artifactId>slf4j-api</artifactId><groupId>org.slf4j</groupId></exclusion><exclusion><groupId>net.sf.ehcache</groupId><artifactId>ehcache-core</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId><version>4.3.5.Final</version></dependency><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-validator</artifactId><version>5.0.3.Final</version></dependency><!-- hibernate end --><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-jpa</artifactId><version>1.9.4.RELEASE</version></dependency></dependencies><build><finalName>test</finalName></build> </project> View Code4.簡(jiǎn)單的測(cè)試
寫(xiě)一個(gè)簡(jiǎn)單的Controller,放在src/main/java文件夾下。然后寫(xiě)一個(gè)hello.jsp文件或者h(yuǎn)ello.ftl文件放在WEB-INF/views目錄下,因?yàn)樵趕pring-mvc-config.xml中已經(jīng)指定了<property name="templateLoaderPath" value="/WEB-INF/views/" />(freemarker視圖解析器) 和? <property name="prefix" value="/WEB-INF/views/" />(InternalResourceViewResolver視圖解析器)視圖文件的位置。
Controller
import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controllerpublic class HelloController {@RequestMapping("/hello")public ModelAndView hello(){ModelAndView mv =new ModelAndView();mv.addObject("spring", "spring mvc");mv.setViewName("hello");return mv;}} View Codehello.jsp
<!DOCTYPE html><html><head><meta charset="utf-8"><title>sprint hello</title></head><body>hello ${spring}!</body></html> View Code5.常見(jiàn)問(wèn)題解決
a)? 工程項(xiàng)目有小紅叉,但是卻找不到錯(cuò)誤
window->show view->problems, 查看錯(cuò)誤如下:
Dynamic Web Module 3.0 requires Java 1.6 or newer.??? test ? ???? line 1?? ?Maven Java EE Configuration Problem
????? Java compiler level does not match the version of the installed Java project facet.??? test? Unknown?? ?Faceted Project Problem (Java Version Mismatch)
解決辦法:
在pom.xml中添加如下代碼, 然后右鍵項(xiàng)目->maven->update project
<profiles><profile><id>jdk-1.7</id><!-- 另外一種激活方式 --><activation><activeByDefault>true</activeByDefault><jdk>1.7</jdk></activation><properties><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target> <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion></properties></profile></profiles>?
b) 用tomcat啟動(dòng)工程時(shí)出現(xiàn) 如下的錯(cuò)誤:
嚴(yán)重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListenerat org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305)at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1139)at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:518)at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:499)at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4733)at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5251)at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)at java.util.concurrent.FutureTask.run(Unknown Source)at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)at java.lang.Thread.run(Unknown Source)解決辦法:右鍵項(xiàng)目->properties->Deployment Assembly, 然后添加 maven dependencies
?
c) 采用uuid主鍵生成策略遇到的問(wèn)題
No generator named "uuid" is defined in the persistence unit
解決辦法:右鍵項(xiàng)目->properties->JPA->Errors/Warnings, 或者 window->preferences->java persistence->JPA->Errors/Warnings
d)maven工程在tomcat中的結(jié)構(gòu)
maven工程的src/main/webapp 中的內(nèi)容會(huì)在tomcat項(xiàng)目的根目錄下,還有就是maven工程的target中的classes文件夾會(huì)在tomcat項(xiàng)目的根目錄下。
轉(zhuǎn)載于:https://www.cnblogs.com/hujunzheng/p/5450255.html
總結(jié)
以上是生活随笔為你收集整理的springmvc环境搭建以及常见问题解决的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux ssh 远程会话保存,远程S
- 下一篇: 洁立汾是立白公司的产品吗?