zoukankan      html  css  js  c++  java
  • 搭建一个Web应用

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

    使用gradle作为构建工具

    build.gradle

     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 }
    build.gradle

    web.xml

     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>
    web.xml

    Spring初始化

    AppInit.java

     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 }
    AppInit.java

    AppRootConf.java

     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 }
    AppRootConf.java

    AppServletConf.java

     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 }
    AppServletConf.java

    配置文件

     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>
    applicationContext.xml
     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>
    applicationContext-mvc.xml
     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>
    log4j2.xml
  • 相关阅读:
    关于lockkeyword
    关于多层for循环迭代的效率优化问题
    Android 面试精华题目总结
    Linux基础回想(1)——Linux系统概述
    linux源代码编译安装OpenCV
    校赛热身 Problem C. Sometimes Naive (状压dp)
    校赛热身 Problem C. Sometimes Naive (状压dp)
    校赛热身 Problem B. Matrix Fast Power
    校赛热身 Problem B. Matrix Fast Power
    集合的划分(递推)
  • 原文地址:https://www.cnblogs.com/sherrykid/p/6225175.html
Copyright © 2011-2022 走看看