• Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker)


    Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker)

     一、准备工作

      1.Eclipse Java EE IDE(4.4.1)

      2.JDK

      3.Tomcat

      4.Maven  maven安装指南

    二、构建工程

      1.file->new->Project

        

      2.next

        

      3. next

        

      4.next

        

      5.finish

        工程目录如下

        

    三、完善项目

        首先,完善目录,增加重要的source Folder,这个不是简单的Floder,这些文件夹是会参与编译的。

        增加src/main/java,src/test/resources,src/test/java目录。让目录变成标准的maven结构

        说明:src/test/resources 通过添加source Folder  添加 

             src/main/java,src/test/java 通过Navigator中 new folder 添加

        window->show view->Navigator

        

        标准目录为:

        

    四、工程属性设置

        1.工程右键->Properties->Deployment Assembly

          修改为

          

    五、SpringMVC工程搭建(MyBatis+Freemarker)

      1.完善工程目录结构  

        在src/main/java文件夹中,新建包

           com.operate.command(存放传入的参数javabean),

           com.operate.dto(存放数据库取出的数据的javabean),

                   com.operate.dao(存放spring与mybatis连接接口),

           com.operate.persistence(存放中间持续的javabean),

                       com.operate.service(service接口),

           com.operate.service.impl(service接口的实现),

           com.operate.controller(存放控制层controller)

        在src/main/resource文件夹中,新建包

          conf(存放配置文件),

          mapper(mybatis的mapper文件)

        在src/test/java文件夹中,新建包

          com.operate.test(存放测试文件)

        在WEB-INF文件夹下新建ftl文件夹(存放freemarker文件)

        这样项目结构基本完成了,如下图:

        

      2.引入依赖包  

        打开maven的pom文件,对本次开发所需使用的架包依次导入(maven项目管理的优势)

        查找依赖结构有个不错的网站,http://search.maven.org/   只要输入包名即可查找引来关系

        pom.xml

      1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      3   <modelVersion>4.0.0</modelVersion>
      4   <groupId>com.sheyou</groupId>
      5   <artifactId>operate</artifactId>
      6   <packaging>war</packaging>
      7   <version>0.0.1-SNAPSHOT</version>
      8   <name>operate Maven Webapp</name>
      9   <url>http://maven.apache.org</url>
     10   <properties>
     11         <!-- spring版本号 -->
     12         <spring.version>4.1.6.RELEASE</spring.version>
     13         <!-- mybatis版本号 -->
     14         <mybatis.version>3.2.8</mybatis.version>
     15         <!-- log4j日志文件管理包版本 -->
     16         <slf4j.version>1.7.7</slf4j.version>
     17         <log4j.version>1.2.17</log4j.version>
     18   </properties>
     19   <dependencies>
     20     <dependency>
     21       <groupId>junit</groupId>
     22       <artifactId>junit</artifactId>
     23       <version>4.4</version>
     24       <scope>test</scope>
     25     </dependency>
     26     <!-- spring jar -->
     27     <dependency>
     28         <groupId>org.springframework</groupId>
     29         <artifactId>spring-core</artifactId>
     30         <version>${spring.version}</version>
     31     </dependency>
     32     <dependency>
     33         <groupId>org.springframework</groupId>
     34         <artifactId>spring-context</artifactId>
     35         <version>${spring.version}</version>
     36     </dependency>
     37     <dependency>
     38         <groupId>org.springframework</groupId>
     39         <artifactId>spring-context-support</artifactId>
     40         <version>${spring.version}</version>
     41     </dependency>
     42     <dependency>
     43         <groupId>org.springframework</groupId>
     44         <artifactId>spring-tx</artifactId>
     45         <version>${spring.version}</version>
     46     </dependency>
     47     <dependency>
     48         <groupId>org.springframework</groupId>
     49         <artifactId>spring-aop</artifactId>
     50         <version>${spring.version}</version>
     51     </dependency>
     52     <dependency>
     53         <groupId>org.springframework</groupId>
     54         <artifactId>spring-beans</artifactId>
     55         <version>${spring.version}</version>
     56     </dependency>
     57     <dependency>
     58         <groupId>org.springframework</groupId>
     59         <artifactId>spring-jdbc</artifactId>
     60         <version>${spring.version}</version>
     61     </dependency>
     62 
     63     <!-- spring mvc jar -->
     64     <dependency>
     65         <groupId>org.springframework</groupId>
     66         <artifactId>spring-web</artifactId>
     67         <version>${spring.version}</version>
     68     </dependency>
     69     <dependency>
     70         <groupId>org.springframework</groupId>
     71         <artifactId>spring-webmvc</artifactId>
     72         <version>${spring.version}</version>
     73     </dependency>
     74     <dependency>
     75         <groupId>org.freemarker</groupId>
     76         <artifactId>freemarker</artifactId>
     77         <version>2.3.20</version>
     78     </dependency>
     79     
     80     <!-- mybatis 包 -->
     81     <dependency>
     82         <groupId>org.mybatis</groupId>
     83         <artifactId>mybatis</artifactId>
     84         <version>3.2.8</version>
     85     </dependency>
     86     <!--mybatis spring 插件 -->
     87     <dependency>
     88         <groupId>org.mybatis</groupId>
     89         <artifactId>mybatis-spring</artifactId>
     90         <version>1.2.2</version>
     91     </dependency>
     92     <!--sqlserver驱动包-->
     93     <dependency>
     94        <groupId>com.microsoft.sqlserver</groupId>
     95        <artifactId>sqljdbc4</artifactId>
     96        <version>3.0</version>
     97     </dependency>
     98
        <!-- mysql驱动包 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.29</version>
            </dependency>
     99     <!-- 导入java ee jar 包 -->
    100     <dependency>
    101         <groupId>javax</groupId>
    102         <artifactId>javaee-api</artifactId>
    103         <version>7.0</version>
    104     </dependency>
    105     
    106     <!-- 日志文件管理包 -->
    107     <!-- log start -->
    108     <dependency>
    109         <groupId>log4j</groupId>
    110         <artifactId>log4j</artifactId>
    111         <version>${log4j.version}</version>
    112     </dependency>
    113     
    114     
    115     <!-- 格式化对象,方便输出日志 -->
    116     <dependency>
    117         <groupId>com.alibaba</groupId>
    118         <artifactId>fastjson</artifactId>
    119         <version>1.1.41</version>
    120     </dependency>
    121 
    122 
    123     <dependency>
    124         <groupId>org.slf4j</groupId>
    125         <artifactId>slf4j-api</artifactId>
    126         <version>${slf4j.version}</version>
    127     </dependency>
    128 
    129     <dependency>
    130         <groupId>org.slf4j</groupId>
    131         <artifactId>slf4j-log4j12</artifactId>
    132         <version>${slf4j.version}</version>
    133     </dependency>
    134     <!-- log end -->
    135     
    136     <dependency>
    137         <groupId>org.apache.commons</groupId>
    138         <artifactId>commons-dbcp2</artifactId>
    139         <version>2.1</version>
    140     </dependency>
    147     
    148     <!-- 阿里巴巴数据源包 -->
    149     <dependency>
    150         <groupId>com.alibaba</groupId>
    151         <artifactId>druid</artifactId>
    152         <version>1.0.9</version>
    153     </dependency>
    154   </dependencies>
    188   <build>
    189     <finalName>operate</finalName>
    190   </build>
    191 </project>

    3.配置数据库连接属性

    conf/ jdbc.properties(jdbc配置文件) 

    SQLServver

    url=jdbc:sqlserver://localhost:1433;DatabaseName=user
    user=root password=root driver=com.microsoft.sqlserver.jdbc.SQLServerDriver

    MySQL

    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/mydays?useUnicode=true&characterEncoding=utf-8
    user=root
    password=root
    

    conf/spring-mybatis.xml(spring与mybatis连接属性)

    <?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:p="http://www.springframework.org/schema/p"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util-3.2.xsd">
        
    	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
    	    destroy-method="close" >
    	    <property name="driverClassName">
    	      <value>${driver}</value>
    	    </property>
    	    <property name="url">
    	      <value>${url}</value>
    	    </property>
    	    <property name="username">
    	      <value>${user}</value>
    	    </property>
    	    <property name="password">
    	      <value>${password}</value>
    	    </property>
    	    <!-- 连接池最大使用连接数 -->
    	    <property name="maxActive">
    	      <value>20</value>
    	    </property>
    	    <!-- 初始化连接大小 -->
    	    <property name="initialSize">
    	      <value>1</value>
    	    </property>
    	    <!-- 获取连接最大等待时间 -->
    	    <property name="maxWait">
    	      <value>60000</value>
    	    </property>
    	    <!-- 连接池最大空闲 -->
    	    <property name="maxIdle">
    	      <value>20</value>
    	    </property>
    	    <!-- 连接池最小空闲 -->
    	    <property name="minIdle">
    	      <value>3</value>
    	    </property>
    	    <!-- 自动清除无用连接 -->
    	    <property name="removeAbandoned">
    	      <value>true</value>
    	    </property>
    	    <!-- 清除无用连接的等待时间 -->
    	    <property name="removeAbandonedTimeout">
    	      <value>180</value>
    	    </property>
    	    <!-- 连接属性 -->
    	    <property name="connectionProperties">
    	      <value>clientEncoding=UTF-8</value>
    	    </property>
    	</bean>
    	     
    	<!-- mybatis文件配置,扫描所有mapper文件 -->
    	<bean id="sqlSessionFactory"
    	    class="org.mybatis.spring.SqlSessionFactoryBean"
    	    p:dataSource-ref="dataSource"
    	    p:configLocation="classpath:conf/mybatis-config.xml"
    	    p:mapperLocations="classpath:mapper/*.xml"/><!-- configLocation为mybatis属性 mapperLocations为所有mapper-->
    	       
    	<!-- spring与mybatis整合配置,扫描所有dao BasePackage dao包-->
    	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
    	       p:basePackage="com.operate.dao"
    	       p:sqlSessionFactoryBeanName="sqlSessionFactory"/>
    	  
    	<!-- 对数据源进行事务管理 -->
    	<bean id="transactionManager"
    	      class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
    	      p:dataSource-ref="dataSource"/>
    </beans>
    

    4.java代码编写

    com.operate.dto/User.java

    package com.operate.dto;
    
    public class User {
    	private String name;
    	private String photo;
    	public String getPhoto() {
    		return photo;
    	}
    	public void setPhoto(String photo) {
    		this.photo = photo;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    }
    

     com.operate.dao/UserDao.java

    1 package com.operate.dao;
    2 
    3 import com.operate.dto.User;
    4 
    5 public interface UserDao {
    6     public User getUser();
    7 }

    com.operate.service/UserService.java

    1 package com.operate.service;
    2 
    3 import com.operate.dto.User;
    4 
    5 public interface UserService {
    6     public User getUser();
    7 }

    com.operate.service.impl/UserServiceImpl.java

     1 package com.operate.service.impl;
     2 
     3 import org.springframework.beans.factory.annotation.Autowired;
     4 import org.springframework.stereotype.Service;
     5 
     6 import com.operate.dao.UserDao;
     7 import com.operate.dto.User;
     8 import com.operate.service.UserService;
     9 
    10 @Service
    11 public class UserServiceImple implements UserService {
    12 
    13     @Autowired
    14     private UserDao userDAO;
    15     
    16     @Override
    17     public User getUser() {
    18         // TODO Auto-generated method stub
    19         return userDAO.getUser();
    20     }
    21 
    22 }

    5.mybatis配置

    conf/mybatis-config.xml(mybatis配置的基本文件)

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration 
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
     	<typeAliases>
     		<typeAlias type="com.operate.dto.User" alias="UserInfo"/>
    	</typeAliases>
        <!-- 映射map -->
        <mappers>
        </mappers>
    </configuration>
    

     mapper/sql-map-User.xml(mybatis的实现)

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
     PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.operate.dao.UserDao">
        <select id="getUser" resultType="UserInfo">
            SELECT 
                *        
         from Member where name='11111111'

      </select> </mapper>

    这样就基本完成了获取用户的功能!

    6.junit测试功能

     1 package com.operate.test;
     2 
     3 import org.junit.Before;
     4 import org.junit.Test;
     5 import org.springframework.context.ApplicationContext;
     6 import org.springframework.context.support.ClassPathXmlApplicationContext;
     7 
     8 import com.operate.dto.User;
     9 import com.operate.service.UserService;
    10 
    11 public class UserTest {
    12     private UserService userService;
    13      @Before
    14     public void before(){                                                                    
    15         @SuppressWarnings("resource")
    16         ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:conf/spring.xml"
    17                 ,"classpath:conf/spring-mybatis.xml"});
    18         userService = (UserService) context.getBean("userServiceImpl");//注意userServiceImpl 首字母要小写
    19     }
    20      
    21     @Test
    22     public void getUser(){
    23         User user = userService.getUser();
    24         System.out.println(user.getName()+":"+user.getPhoto());
    25     }
    26 }

     测试结果:

    7.SpringMVC搭建

    Web.XML

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns="http://java.sun.com/xml/ns/javaee"   
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
     4          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
     5                              http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   
     6          version="2.5" >
     7   <display-name>Sheyou-Operate</display-name>
     8   <!-- 加载其他配置文件至Spring应用上下文中 -->
     9   <listener>
    10       <listener-class>
    11           org.springframework.web.context.ContextLoaderListener
    12       </listener-class>
    13   </listener>
    14   <!-- 指定多个Spring配置文件  -->
    15   <context-param>
    16       <param-name>contextConfigLocation</param-name>
    17       <param-value>
    18           classpath:conf/spring.xml;
    19           classpath:conf/spring-mybatis.xml
    20       </param-value>
    21   </context-param>
    22   <!-- 字符集 过滤器  -->  
    23   <filter>  
    24       <filter-name>CharacterEncodingFilter</filter-name>  
    25       <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
    26       <init-param>  
    27           <param-name>encoding</param-name>  
    28           <param-value>UTF-8</param-value>  
    29       </init-param>  
    30       <init-param>  
    31           <param-name>forceEncoding</param-name>  
    32           <param-value>true</param-value>  
    33       </init-param>  
    34   </filter>  
    35   <filter-mapping>  
    36       <filter-name>CharacterEncodingFilter</filter-name>  
    37       <url-pattern>*.html</url-pattern>  
    38   </filter-mapping>
    39   
    40   
    41   <!-- Spring view分发器 -->
       <!-- servlet-name 决定了初始化的名字:name-servlet.xml --> 42 <servlet> 43 <servlet-name>dispatcher</servlet-name> 44 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 45 <init-param> 46 <param-name>contextConfigLocation</param-name> 47 <param-value>/WEB-INF/classes/conf/dispatcher-servlet.xml</param-value> 48 </init-param> 49 <load-on-startup>1</load-on-startup> 50 </servlet> 51 <servlet-mapping> 52 <servlet-name>dispatcher</servlet-name> 53 <url-pattern>/</url-pattern> 54 </servlet-mapping> 55 56 <welcome-file-list> 57 <welcome-file>index.jsp</welcome-file> 58 </welcome-file-list> 59 <error-page> 60 <error-code>400</error-code> 61 <location>/error/400.jsp</location> 62 </error-page> 63 <error-page> 64 <error-code>403</error-code> 65 <location>/error/403.jsp</location> 66 </error-page> 67 <error-page> 68 <error-code>404</error-code> 69 <location>/error/404.jsp</location> 70 </error-page> 71 <error-page> 72 <error-code>405</error-code> 73 <location>/error/405.jsp</location> 74 </error-page> 75 <error-page> 76 <error-code>406</error-code> 77 <location>/error/406.jsp</location> 78 </error-page> 79 <error-page> 80 <error-code>412</error-code> 81 <location>/error/412.jsp</location> 82 </error-page> 83 <error-page> 84 <error-code>500</error-code> 85 <location>/error/500.jsp</location> 86 </error-page> 87 <error-page> 88 <error-code>501</error-code> 89 <location>/error/501.jsp</location> 90 </error-page> 91 <error-page> 92 <error-code>502</error-code> 93 <location>/error/502.jsp</location> 94 </error-page> 95 <error-page> 96 <error-code>503</error-code> 97 <location>/error/503.jsp</location> 98 </error-page> 99 </web-app>

    dispatcher-servlet.xml

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"   
     3        xmlns:aop="http://www.springframework.org/schema/aop"   
     4        xmlns:context="http://www.springframework.org/schema/context"  
     5        xmlns:mvc="http://www.springframework.org/schema/mvc"   
     6        xmlns:tx="http://www.springframework.org/schema/tx"   
     7        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
     8        xsi:schemaLocation="http://www.springframework.org/schema/aop   
     9         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
    10         http://www.springframework.org/schema/beans   
    11         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
    12         http://www.springframework.org/schema/context   
    13         http://www.springframework.org/schema/context/spring-context-3.0.xsd   
    14         http://www.springframework.org/schema/mvc   
    15         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd   
    16         http://www.springframework.org/schema/tx   
    17         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    18    
    19     
    20    <mvc:annotation-driven/>
    21     
    22    <context:component-scan base-package="com.operate.controller" />
    23    
    24    <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    25        <property name="prefix" value="/WEB-INF/views/" />  
    26        <property name="suffix" value=".jsp" />
    27    </bean> -->
    28    
    29    <bean id="viewResolver"  
    30       class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  
    31         <!--<property name="prefix" value="/WEB-INF/view/" /> -->
    32         <property name="suffix">  
    33             <value>.ftl</value>  
    34         </property>  
    35         <property name="contentType" value="text/html;charset=UTF-8"></property>  
    36    </bean>
    37    <!-- freemarker的配置 -->
    38    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    39      <property name="templateLoaderPath" value="/WEB-INF/view/ftl/" />
    40       <property name="defaultEncoding" value="utf-8" />
    41       <property name="freemarkerSettings">
    42        <props>
    43         <prop key="template_update_delay">10</prop>
    44         <prop key="locale">zh_CN</prop>
    45         <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
    46         <prop key="date_format">yyyy-MM-dd</prop>
    47         <prop key="number_format">#.####</prop>
    48        </props>
    49       </property>
    50    </bean>
    51    
    52    <!-- 静态资源 -->
    53    <!-- 图片 -->
    54    <mvc:resources mapping="/images/**" location="/images/"
    55         cache-period="31556926" />
    56    <!-- js -->
    57    <mvc:resources mapping="/js/**" location="/js/"
    58         cache-period="31556926" />
    59    <!-- css -->
    60    <mvc:resources mapping="/css/**" location="/css/"
    61         cache-period="31556926" />
    66    <!-- <mvc:resources location="/qrcode/" mapping="//**" /> -->
    67    <!-- 文件上传解析器 id 必须为multipartResolver -->
    68    <!-- SpringMVC在超出上传文件限制时,会抛出org.springframework.web.multipart.MaxUploadSizeExceededException -->
    69    <!-- 该异常是SpringMVC在检查上传的文件信息时抛出来的,而且此时还没有进入到Controller方法中 -->
    70    <bean id="multipartResolver" 
    71          class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
    72          <!-- <property name="maxUploadSize" value="10485760"/> -->
    73    </bean>
    74    
    75    <!--定义异常处理页面-->
    76 
    77     <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    78         <property name="exceptionMappings">
    79             <props>
    80                 <prop key="java.sql.SQLException">exception</prop>
    81                 <prop key="java.io.IOException">exception</prop>
    82                 <prop key="java.lang.IOException">exception</prop>
    83                 <prop key="Open Declaration com.google.zxing.WriterException">exception</prop>
    84                 <prop key="java.lang.NullPointerException">exception</prop>
    85             </props>
    86         </property>
    87     </bean>
    88    
    89 </beans>

    spring.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <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" xsi:schemaLocation="
     3 http://www.springframework.org/schema/beans
     4 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     5 http://www.springframework.org/schema/context
     6 http://www.springframework.org/schema/context/spring-context-3.0.xsd
     7 ">
     8 
     9     <!-- 引入属性文件 -->
    10     <context:property-placeholder location="classpath:conf/jdbc.properties" />
    11     <!-- 自动扫描(自动注入) -->
    12     <context:component-scan base-package="com.operate.service" />
    13 
    14 </beans>

    8.log4j日志记录

     1 ### set log levels ###
     2 #log4j.rootLogger = debug , stdout , D , E
     3 log4j.rootLogger = debug , stdout , D
     4  
     5 ###  output to the console ###
     6 log4j.appender.stdout = org.apache.log4j.ConsoleAppender
     7 log4j.appender.stdout.Target = System.out
     8 log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
     9 #log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
    10 log4j.appender.stdout.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n
    11  
    12 ### Output to the log file ###
    13 log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
    14 log4j.appender.D.File = ../../WEB-INF/logs/log.log
    15 log4j.appender.D.Append = true
    16 log4j.appender.D.Threshold = DEBUG 
    17 log4j.appender.D.layout = org.apache.log4j.PatternLayout
    18 log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
    19  
    20 ### Save exception information to separate file ###
    21 log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
    22 log4j.appender.D.File = ../../WEB-INF/logs/error.log 
    23 log4j.appender.D.Append = true
    24 log4j.appender.D.Threshold = ERROR 
    25 log4j.appender.D.layout = org.apache.log4j.PatternLayout
    26 log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

     9.测试

     WEB-INF/view/ftl/index.ftl(测试文件)

    1 <html>
    2 <head>
    3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    4 <title>Insert title here</title>
    5 </head>
    6 <body>
    7 <h1>欢迎你!!!${result}</h1>
    8 </body>
    9 </html>

    com.operate/controller/IndexComtroller.java(controller层控制)

     1 package com.operate.controller;
     2 
     3 import javax.annotation.Resource;
     4 import javax.servlet.http.HttpServletRequest;
     5 import javax.servlet.http.HttpServletResponse;
     6 
     7 import org.springframework.stereotype.Controller;
     8 import org.springframework.web.bind.annotation.RequestMapping;
     9 import org.springframework.web.servlet.ModelAndView;
    10 
    11 import com.operate.dto.User;
    12 import com.operate.service.UserService;
    13 
    14 
    15 @Controller
    16 public class IndexController {
    17     @Resource(name = "userService")
    18     private UserService userService;
    19     
    20     @RequestMapping("/index.html")
    21     ModelAndView index(HttpServletRequest request,HttpServletResponse response)
    22     {
    23         User user = userService.getUser();
    24         return new ModelAndView("index")
    25         .addObject("result", user.getName());
    26     }
    27 }

    userServiceImpl.class 修改

     1 package com.operate.service.impl;
     2 
     3 import org.springframework.beans.factory.annotation.Autowired;
     4 import org.springframework.stereotype.Service;
     5 
     6 import com.operate.dao.UserDao;
     7 import com.operate.dto.User;
     8 import com.operate.service.UserService;
     9 
    10 @Service("userService")
    11 public class UserServiceImpl implements UserService {
    12 
    13     @Autowired
    14     private UserDao userDao;
    15     
    16     @Override
    17     public User getUser() {
    18         // TODO Auto-generated method stub
    19         return userDao.getUser();
    20     }
    21 
    22 }

    部署到tomcat中

    浏览器中输入http://localhost:8080/operate/index.html

    参考博客

  • 相关阅读:
    六十:权限提升-MY&MS&ORA等SQL数据库提权
    五十九:权限提升-Win溢出漏洞及AT&SC&PS提权
    五十八:权限提升-网站权限后台漏洞地第三方获取
    在kaili装git与git-lfs
    机器学习中的最优化算法总结
    动态规划及意义
    对于中变量与类的使用感想
    安装awvs教程参照
    kali乱码问题
    linux与Mac互相粘贴存在障碍,可以用这个命令联系起来
  • 原文地址:https://www.cnblogs.com/dawnheaven/p/4774681.html
Copyright © 2020-2023  润新知