搭建一个Web应用

因为EasyUI会涉及到与后台数据的交互,所以使用Spring MVC作为后台,搭建一个完整的Web环境

使用gradle作为构建工具

build.gradle

 group 'org.zln.lkd'
version '1.0-SNAPSHOT' apply plugin: 'jetty' sourceCompatibility = 1.8 repositories {
mavenLocal()
mavenCentral()
} dependencies {
compile(
"org.slf4j:slf4j-api:1.7.21",
"org.apache.logging.log4j:log4j-slf4j-impl:2.7",
"org.apache.logging.log4j:log4j-core:2.7",
"org.apache.logging.log4j:log4j-api:2.7",
"org.apache.commons:commons-lang3:3.3.2",
"org.springframework:spring-context:4.3.1.RELEASE",
"org.springframework:spring-aop:4.3.1.RELEASE",
"org.springframework:spring-core:4.3.1.RELEASE",
"org.springframework:spring-expression:4.3.1.RELEASE",
"org.springframework:spring-beans:4.3.1.RELEASE",
"org.springframework:spring-webmvc:4.3.1.RELEASE",
"org.springframework:spring-web:4.3.1.RELEASE",
"org.springframework:spring-jdbc:4.3.1.RELEASE",
"org.springframework:spring-tx:4.3.1.RELEASE",
"org.springframework:spring-test:4.3.1.RELEASE",
"org.springframework:spring-aspects:4.3.1.RELEASE",
"mysql:mysql-connector-java:5.1.32",
"com.alibaba:druid:1.0.9",
"org.mybatis:mybatis:3.4.1",
"org.mybatis:mybatis-spring:1.3.0",
"com.github.pagehelper:pagehelper:4.1.6",
"javax.servlet:javax.servlet-api:4.0.0-b01",
"jstl:jstl:1.2",
"com.fasterxml.jackson.core:jackson-databind:2.8.1"
) testCompile(
"junit:junit:4.12"
)
} // 自动创建好src目录 包括源码与测试源码
task mkdirs << {
sourceSets*.java.srcDirs*.each { it.mkdirs() }
sourceSets*.resources.srcDirs*.each { it.mkdirs() }
} // 显示当前项目下所有用于 compile 的 jar.
task listJars(description: 'Display all compile jars.') << {
configurations.compile.each { File file -> println file.name }
}

build.gradle

web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"> <!--过滤器设置请求编码-->
<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-8</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>

web.xml

Spring初始化

AppInit.java

 package conf.spring;

 import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

 /**
* Spring初始化类
* Created by sherry on 16/11/28.
*/
public class AppInit extends AbstractAnnotationConfigDispatcherServletInitializer { /**
* Spring后台配置类
* @return
*/
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{AppRootConf.class};
} /**
* Spring MVC配置类
* @return
*/
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{AppServletConf.class};
} /**
* 拦截地址呗Spring MVC处理
* @return
*/
@Override
protected String[] getServletMappings() {
return new String[]{"*.json","*.html","*.do","*.action","*.ajax"};
}
}

AppInit.java

AppRootConf.java

 package conf.spring;

 import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; /**
* Created by sherry on 16/11/28.
*/
@Configuration
//排除Spring MVC注解类
@ComponentScan(basePackages = {"org.zln"}, excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class)})
@ImportResource("classpath:applicationContext.xml")
public class AppRootConf {
}

AppRootConf.java

AppServletConf.java

 package conf.spring;

 import org.springframework.context.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView; /**
* Created by sherry on 16/11/28.
*/
@Configuration
//开启MVC支持,同 <mvc:annotation-driven>
@EnableWebMvc
//仅扫描Controller注解的类
@ComponentScan(value = "org.zln",useDefaultFilters = false,includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = Controller.class)})
@ImportResource("classpath:applicationContext-mvc.xml")
public class AppServletConf extends WebMvcConfigurerAdapter { /**
* 配置JSP视图解析器
* @return
*/
@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver resourceViewResolver = new InternalResourceViewResolver();
resourceViewResolver.setPrefix("/WEB-INF/jsp/");
resourceViewResolver.setSuffix(".jsp");
//JstlView表示JSP模板页面需要使用JSTL标签库,classpath中必须包含jstl的相关jar包;
resourceViewResolver.setViewClass(JstlView.class);
resourceViewResolver.setExposeContextBeansAsAttributes(true);
return resourceViewResolver;
} /**
* 配置静态资源的处理:要求DispatcherServlet将对静态资源的请求转发到Servlet容器默认的Servlet上,
* 而不是使用DispatcherServlet本身来处理
* @param configurer
*/
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
} }

AppServletConf.java

配置文件

 <?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:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> </beans>

applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!--静态资源-->
<mvc:resources mapping="/css/**" location="/WEB-INF/css/"/> <!--<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">-->
<!--<property name="supportedMediaTypes">-->
<!--<list>-->
<!--<value>text/html;charset=UTF-8</value>-->
<!--</list>-->
<!--</property>-->
<!--</bean>-->
<!--<bean id="formHttpMessageConverter" class="org.springframework.http.converter.FormHttpMessageConverter"></bean>--> <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">-->
<!--<property name="messageConverters">-->
<!--<list>-->
<!--<ref bean="formHttpMessageConverter"/>-->
<!--<ref bean="mappingJacksonHttpMessageConverter"/>-->
<!--</list>-->
<!--</property>-->
<!--</bean>-->
</beans>

applicationContext-mvc.xml

 <?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
<!--appenders配置输出到什么地方-->
<appenders>
<!--Console:控制台-->
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</appenders> <loggers>
<!--建立一个默认的root的logger-->
<root level="trace">
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>

log4j2.xml

上一篇:UML软件方法大纲


下一篇:python面试题一个字符串是否由重复的子字符串组成