由於EasyUI會涉及到與後臺數據的交互,因此使用Spring MVC做爲後臺,搭建一個完整的Web環境css
使用gradle做爲構建工具html
build.gradlejava
1 group 'org.zln.lkd' 2 version '1.0-SNAPSHOT' 3 4 apply plugin: 'jetty' 5 6 sourceCompatibility = 1.8 7 8 repositories { 9 mavenLocal() 10 mavenCentral() 11 } 12 13 dependencies { 14 compile( 15 "org.slf4j:slf4j-api:1.7.21", 16 "org.apache.logging.log4j:log4j-slf4j-impl:2.7", 17 "org.apache.logging.log4j:log4j-core:2.7", 18 "org.apache.logging.log4j:log4j-api:2.7", 19 "org.apache.commons:commons-lang3:3.3.2", 20 "org.springframework:spring-context:4.3.1.RELEASE", 21 "org.springframework:spring-aop:4.3.1.RELEASE", 22 "org.springframework:spring-core:4.3.1.RELEASE", 23 "org.springframework:spring-expression:4.3.1.RELEASE", 24 "org.springframework:spring-beans:4.3.1.RELEASE", 25 "org.springframework:spring-webmvc:4.3.1.RELEASE", 26 "org.springframework:spring-web:4.3.1.RELEASE", 27 "org.springframework:spring-jdbc:4.3.1.RELEASE", 28 "org.springframework:spring-tx:4.3.1.RELEASE", 29 "org.springframework:spring-test:4.3.1.RELEASE", 30 "org.springframework:spring-aspects:4.3.1.RELEASE", 31 "mysql:mysql-connector-java:5.1.32", 32 "com.alibaba:druid:1.0.9", 33 "org.mybatis:mybatis:3.4.1", 34 "org.mybatis:mybatis-spring:1.3.0", 35 "com.github.pagehelper:pagehelper:4.1.6", 36 "javax.servlet:javax.servlet-api:4.0.0-b01", 37 "jstl:jstl:1.2", 38 "com.fasterxml.jackson.core:jackson-databind:2.8.1" 39 ) 40 41 testCompile( 42 "junit:junit:4.12" 43 ) 44 } 45 46 // 自動建立好src目錄 包括源碼與測試源碼 47 task mkdirs << { 48 sourceSets*.java.srcDirs*.each { it.mkdirs() } 49 sourceSets*.resources.srcDirs*.each { it.mkdirs() } 50 } 51 52 // 顯示當前項目下全部用於 compile 的 jar. 53 task listJars(description: 'Display all compile jars.') << { 54 configurations.compile.each { File file -> println file.name } 55 }
web.xmlmysql
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 5 version="3.1"> 6 7 <!--過濾器設置請求編碼--> 8 <filter> 9 <filter-name>CharacterEncodingFilter</filter-name> 10 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 11 <init-param> 12 <param-name>encoding</param-name> 13 <param-value>utf-8</param-value> 14 </init-param> 15 </filter> 16 <filter-mapping> 17 <filter-name>CharacterEncodingFilter</filter-name> 18 <url-pattern>/*</url-pattern> 19 </filter-mapping> 20 21 22 23 <welcome-file-list> 24 <welcome-file>index.jsp</welcome-file> 25 </welcome-file-list> 26 27 </web-app>
Spring初始化git
AppInit.javagithub
1 package conf.spring; 2 3 import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 4 5 /** 6 * Spring初始化類 7 * Created by sherry on 16/11/28. 8 */ 9 public class AppInit extends AbstractAnnotationConfigDispatcherServletInitializer { 10 11 12 /** 13 * Spring後臺配置類 14 * @return 15 */ 16 @Override 17 protected Class<?>[] getRootConfigClasses() { 18 return new Class<?>[]{AppRootConf.class}; 19 } 20 21 /** 22 * Spring MVC配置類 23 * @return 24 */ 25 @Override 26 protected Class<?>[] getServletConfigClasses() { 27 return new Class<?>[]{AppServletConf.class}; 28 } 29 30 /** 31 * 攔截地址唄Spring MVC處理 32 * @return 33 */ 34 @Override 35 protected String[] getServletMappings() { 36 return new String[]{"*.json","*.html","*.do","*.action","*.ajax"}; 37 } 38 }
AppRootConf.javaweb
1 package conf.spring; 2 3 import org.springframework.context.annotation.ComponentScan; 4 import org.springframework.context.annotation.Configuration; 5 import org.springframework.context.annotation.FilterType; 6 import org.springframework.context.annotation.ImportResource; 7 import org.springframework.web.servlet.config.annotation.EnableWebMvc; 8 9 /** 10 * Created by sherry on 16/11/28. 11 */ 12 @Configuration 13 //排除Spring MVC註解類 14 @ComponentScan(basePackages = {"org.zln"}, excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class)}) 15 @ImportResource("classpath:applicationContext.xml") 16 public class AppRootConf { 17 }
AppServletConf.javaajax
1 package conf.spring; 2 3 import org.springframework.context.annotation.*; 4 import org.springframework.stereotype.Controller; 5 import org.springframework.web.servlet.ViewResolver; 6 import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; 7 import org.springframework.web.servlet.config.annotation.EnableWebMvc; 8 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 9 import org.springframework.web.servlet.view.InternalResourceViewResolver; 10 import org.springframework.web.servlet.view.JstlView; 11 12 /** 13 * Created by sherry on 16/11/28. 14 */ 15 @Configuration 16 //開啓MVC支持,同 <mvc:annotation-driven> 17 @EnableWebMvc 18 //僅掃描Controller註解的類 19 @ComponentScan(value = "org.zln",useDefaultFilters = false,includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = Controller.class)}) 20 @ImportResource("classpath:applicationContext-mvc.xml") 21 public class AppServletConf extends WebMvcConfigurerAdapter { 22 23 /** 24 * 配置JSP視圖解析器 25 * @return 26 */ 27 @Bean 28 public ViewResolver viewResolver(){ 29 InternalResourceViewResolver resourceViewResolver = new InternalResourceViewResolver(); 30 resourceViewResolver.setPrefix("/WEB-INF/jsp/"); 31 resourceViewResolver.setSuffix(".jsp"); 32 //JstlView表示JSP模板頁面須要使用JSTL標籤庫,classpath中必須包含jstl的相關jar包; 33 resourceViewResolver.setViewClass(JstlView.class); 34 resourceViewResolver.setExposeContextBeansAsAttributes(true); 35 return resourceViewResolver; 36 } 37 38 /** 39 * 配置靜態資源的處理:要求DispatcherServlet將對靜態資源的請求轉發到Servlet容器默認的Servlet上, 40 * 而不是使用DispatcherServlet自己來處理 41 * @param configurer 42 */ 43 @Override 44 public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { 45 configurer.enable(); 46 } 47 48 }
配置文件spring
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:p="http://www.springframework.org/schema/p" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-4.3.xsd 11 http://www.springframework.org/schema/tx 12 http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> 13 14 15 </beans>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-4.1.xsd 11 http://www.springframework.org/schema/mvc 12 http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> 13 <!--靜態資源--> 14 <mvc:resources mapping="/css/**" location="/WEB-INF/css/"/> 15 16 <!--<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">--> 17 <!--<property name="supportedMediaTypes">--> 18 <!--<list>--> 19 <!--<value>text/html;charset=UTF-8</value>--> 20 <!--</list>--> 21 <!--</property>--> 22 <!--</bean>--> 23 <!--<bean id="formHttpMessageConverter" class="org.springframework.http.converter.FormHttpMessageConverter"></bean>--> 24 25 <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">--> 26 <!--<property name="messageConverters">--> 27 <!--<list>--> 28 <!--<ref bean="formHttpMessageConverter"/>--> 29 <!--<ref bean="mappingJacksonHttpMessageConverter"/>--> 30 <!--</list>--> 31 <!--</property>--> 32 <!--</bean>--> 33 </beans>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <configuration status="OFF"> 3 <!--appenders配置輸出到什麼地方--> 4 <appenders> 5 <!--Console:控制檯--> 6 <Console name="Console" target="SYSTEM_OUT"> 7 <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> 8 </Console> 9 </appenders> 10 11 <loggers> 12 <!--創建一個默認的root的logger--> 13 <root level="trace"> 14 <appender-ref ref="Console"/> 15 </root> 16 </loggers> 17 </configuration>