springmvc环境搭建以及常见问题解决
1.新建maven工程
a)? 打開eclipse,file->new->project->Maven->Maven Project
?b)? 下一步
c) ? 選擇創建的工程為webapp,下一步
?d) ? 填寫項目的group id和artifact id。一般情況下,group id寫域名的倒序,artifact id寫項目名稱即可。最后點完成。
e)?? 最初建好后,項目目錄結構如下
f)?? 一般的項目目錄中,還有src/main/java,src/main/test/java,src/main/test/resources這 三個source folder,需要手動創建。
?
2. 修改項目基本設置
a)??? 右鍵此項目名稱->Properties->Java Build path,點擊source標簽。
?
?b)? 將上missing的文件夾刪除,然后重新添加,如下:
?c)?? 重新添加之后的效果如下:
d)?? 如果某些folder不想 build path,直接remove就行了(本人只選擇了src/main/java, 和 src/main/resources),最終如下:
?e)? 修改jre系統
?
?f)? 修改java compiler compliance level 與 jre系統的level一致
?g) 修改Project Facets
Dynamic Web Module無法在這里直接修改為3.0,需要打開工程目錄下有一個.settings文件夾,打開org.eclipse.wst.common.project.facet.core.xml,做如下修改:
<installed facet="jst.web" version="3.0"/>重啟eclipe就可以看到更改生效了。
3.必要的配置文件
?在Java Resources/scr/main/resources目錄下,創建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 自動注冊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配置 ,值越小就優先解析,這里的配置是 先找 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環境搭建</description><!-- 加載Spring配置文件 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:/config/applicationContext.xml</param-value></context-param><!-- Spring監聽 --><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 請求后綴 --><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, 最終效果圖如下
注:其實測試的話沒有必要依賴這么多了,只需要將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.簡單的測試
寫一個簡單的Controller,放在src/main/java文件夾下。然后寫一個hello.jsp文件或者hello.ftl文件放在WEB-INF/views目錄下,因為在spring-mvc-config.xml中已經指定了<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.常見問題解決
a)? 工程項目有小紅叉,但是卻找不到錯誤
window->show view->problems, 查看錯誤如下:
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中添加如下代碼, 然后右鍵項目->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啟動工程時出現 如下的錯誤:
嚴重: 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)解決辦法:右鍵項目->properties->Deployment Assembly, 然后添加 maven dependencies
?
c) 采用uuid主鍵生成策略遇到的問題
No generator named "uuid" is defined in the persistence unit
解決辦法:右鍵項目->properties->JPA->Errors/Warnings, 或者 window->preferences->java persistence->JPA->Errors/Warnings
d)maven工程在tomcat中的結構
maven工程的src/main/webapp 中的內容會在tomcat項目的根目錄下,還有就是maven工程的target中的classes文件夾會在tomcat項目的根目錄下。
轉載于:https://www.cnblogs.com/hujunzheng/p/5450255.html
總結
以上是生活随笔為你收集整理的springmvc环境搭建以及常见问题解决的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux ssh 远程会话保存,远程S
- 下一篇: springmvc+jpa实现分页的两种