SpringMVC+Freemarker环境搭建

一:web.xml文件配置


<servlet>
   <servlet-name>springmvc</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-mvn.xml</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>

</servlet-mapping>

添加编码过滤器

<filter>
        <filter-name>encoding</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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
</filter>
<filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>

logback集成到SpringMVC项目中

<context-param> 
        <param-name>logbackConfigLocation</param-name> 
        <param-value>classpath:logback.xml</param-value> 
</context-param> 
<listener> 
        <listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class> 
</listener>

二:spring-mvc.xml文件配置


<context:component-scan base-package="com.ultrapower.bjd.web">

</context:component-scan>

<mvc:default-servlet-handler />

配置freeMarker的模板配置信息

<bean id="freemarkerConfig"
  class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
  <property name="templateLoaderPaths">
       <array>
          <value>/WEB-INF/freemarker/</value>
          <value>/WEB-INF/freemarker/mgr/</value>
          <value>/WEB-INF/freemarker/anls/</value>
       </array>
  </property>
  <property name="freemarkerSettings">
     <props>
        <prop key="incompatible_improvements">2.3.22</prop>
        <prop key="template_exception_handler">rethrow</prop>
        <prop key="default_encoding">UTF-8</prop>
        <prop key="locale">zh_CN</prop>
        <prop key="date_format">yyyy-MM-dd</prop>
        <prop key="time_format">HH:mm:ss</prop>
        <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
        <prop key="number_format">0.################</prop>
     </props>
  </property>
</bean>

配置Freemarker视图解析器

<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="prefix" value="" />
    <property name="suffix" value=".html" />
    <property name="contentType" value="text/html;charset=UTF-8" />
    <property name="order" value="0"></property>
    <property name="viewClass"
              value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
</bean>

配置其他视图解析器(处理JSP类型的试图解析器 处理的视图为:InternalResourceView或者JstlView)

<bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="" />
    <property name="suffix" value=".jsp" />
    <property name="contentType" value="text/html;charset=UTF-8" />
    <property name="order" value="1"></property>
</bean>

配置全局的拦截器

<mvc:interceptors>
  <mvc:interceptor>
     <mvc:mapping path="/**"/>
     <bean class="com.ultrapower.bjd.web.interceptor.UserVilitorInterceptor" />
  </mvc:interceptor>
</mvc:interceptors>

其它(使用FastJson)


<mvc:annotation-driven >
       <mvc:message-converters register-defaults="true">
            <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
            <bean id="fastJsonHttpMessageConverter"

class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <!-- 这里顺序不能反,一定先写text/html,不然ie下出现下载提示 -->
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
                <property name="features">
                    <list>
                        <value>WriteMapNullValue</value>
                        <value>QuoteFieldNames</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
</mvc:annotation-driven>

三:使用


@Controller
@RequestMapping(path="/demo")
public class DemoController {
 
 @RequestMapping(path="/getReportData.action")
 public ModelAndView getReportData(
       String objectType,
       @RequestParam(required=false)String dimension,
       @RequestParam(value="interval",required=false) Integer interval,
       @RequestParam(defaultValue="false")boolean isDownload,
       ModelMap modelMap,
       HttpSession session) {
    if(isDownload){
       modelMap.put("value", null);
       modelMap.put("headers", null);
       return new ModelAndView(new ExcelXlsView(),modelMap) ;
    }else{
       modelMap.put("value", null);
       return new ModelAndView(new FastJsonJsonView(),modelMap) ;
    }
  }

@RequestMapping(value = { "**/{page}.html", "**/{page}.xhtml"})
 public ModelAndView showPage(HttpServletRequest request,
   HttpServletResponse response,
   HttpSession session,
   @PathVariable("page") String page,
   ModelMap modelMap,
   RedirectAttributes attr) throws Exception {
  
      return new ModelAndView("redirect:" + "/404.jsp");
  }

@ResponseBody
@RequestMapping(path="/getData.action")
 public List<String> getData(String objectType) {
       return new ArrayList<String>() ;
  }
}




public class ExcelXlsView extends AbstractXlsView{

@Override
     protected void buildExcelDocument(Map<String, Object> model, Workbook workbook,

HttpServletRequest request,
          HttpServletResponse response) throws Exception {

response.setContentType("application/vnd.ms-excel;charset=utf-8");
              response.setHeader("Content-Disposition", "attachment;filename="

+ URLEncoder.encode(fileName, "UTF-8") + ".xls");
               //TODO

}

}

参考:
AbstractXlsView
AbstractXlsxView
AbstractPdfView
AbstractXlsxStreamingView


部分Maven配置


<properties>
  <spring.version>4.2.4.RELEASE</spring.version>
  <drools.version>6.4.0.Final</drools.version>

</properties>
<dependencies>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-beans</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context-support</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-web</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-tx</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-aop</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-expression</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>1.2.8</version>
  </dependency>
  <dependency>
   <groupId>org.freemarker</groupId>
   <artifactId>freemarker</artifactId>
   <version>2.3.23</version>
  </dependency>
  <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
      <version>1.1.7</version>
  </dependency>
  <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.1.7</version>
  </dependency>
  <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>jcl-over-slf4j</artifactId>
      <version>1.7.21</version>
  </dependency>
  <dependency>
      <groupId>org.logback-extensions</groupId>
      <artifactId>logback-ext-spring</artifactId>
      <version>0.1.1</version>
  </dependency>

</dependencies>

<build>
    <finalName>guest</finalName>
    <resources>
          <resource>
           <directory>src/main/resources</directory>
           <includes>
              <include>**/*.properties</include>
              <include>**/*.xml</include>
              <include>**/*.conf</include>
              <include>**/*.drl</include>
           </includes>
          <filtering>false</filtering>
    </resource>
    <resource>
         <directory>src/main/java</directory>
         <includes>
             <include>**/*</include>
         </includes>
         <filtering>false</filtering>
   </resource>
 </resources>
    <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5</version>
    <configuration>
     <source>1.7</source>
     <target>1.7</target>
     <encoding>UTF-8</encoding>
    </configuration>
   </plugin>
  </plugins>
  <pluginManagement>
       <plugins>
         <plugin>
           <groupId>org.apache.tomcat.maven</groupId>
           <artifactId>tomcat6-maven-plugin</artifactId>
           <version>2.2</version>
           <configuration>
            <server>tomcat6</server>
            <path>/guest</path>
            <charset>UTF-8</charset>
            <uriEncoding>UTF-8</uriEncoding>
           </configuration>
         </plugin>
         <plugin>
           <groupId>org.apache.tomcat.maven</groupId>
           <artifactId>tomcat7-maven-plugin</artifactId>
           <version>2.2</version>
           <configuration>
            <server>tomcat7</server>
            <path>/guest</path>
            <charset>UTF-8</charset>
            <uriEncoding>UTF-8</uriEncoding>
           </configuration>
         </plugin>
       </plugins>
  </pluginManagement>
  </build>
时间: 2024-10-10 13:04:46

SpringMVC+Freemarker环境搭建的相关文章

springMVC初探--环境搭建和第一个HelloWorld简单项目

注:此篇为学习springMVC时,做的笔记整理. MVC框架要做哪些事情? a,将url映射到java类,或者java类的方法上 b,封装用户提交的数据 c,处理请求->调用相关的业务处理—>封装响应的数据 d,将响应的数据进行渲染 springMVC框架有什么优点? 性能比struts2好 简单便捷易学,能进行更简洁的web层开发 天生和spring无缝集成 使用约定优于配置 能够进行简单junit测试 支持restful风格 异常处理,本地化国际化,数据验证,拦截器 提供一套强大的JSP

SpringMvc学习-环境搭建

最近在学习SpringMVC,首先来说说SpringMVC项目的搭建. 1.SpringMVC简介 spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求-响应模型,框架的目的就是帮助我们简化开发,Spring Web MVC也是要简化我们日常Web开发的. 另外还有一种基于组件的.事件驱动的Web框架在此就不介绍了,如Tapestry.JSF等. Sprin

springmvc+mybatis环境搭建

1.spring+mybatis 环境搭建: A.配置jdbc和dbcp数据源:注意版本com.mysql.cj.jdbc.Driver B.配置sessionfactory,绑定dbcp及配置mapper文件命名规范, C.配置mapper文件所在包 *注意调整mysql数据库时区,否则将报错,可参考如下: MySQL之--Ignoring query to other database问题 https://blog.csdn.net/l1028386804/article/details/5

SpringMVC初学-----环境搭建。

Spring接触有一段时间了,但SpringMVC一直没接触过.利用周末的时间学了一点. 环境:eclipse.maven.jetty. 1.在pom.xml文件中加入SpringMVC的依赖. <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.0.5.RELEASE</ver

SpringMVC + MyBatis 环境搭建(转)

本文转自:http://blog.csdn.net/zoutongyuan/article/details/41379851 源码地址:https://github.com/starzou/quick4j 点击打开 看我们的项目结构: 是一个典型的Maven 项目 : src/main/java:存放java源文件src/main/resources:存放程序资源.配置文件src/test/java:存放测试代码文件src/main/webapp:web根目录pom.xml : maven项目配

Spring+Springmvc+Hibernate环境搭建与配置

这里给出JavaWeb项目Spring+Springmvc+Hibernate框架环境的搭建与配置,方便以后快速创建项目.下面是以本人的一个项目为例给出的配置,不同项目具体的结构都是可以按一下结构完成的,至于配置内容则需要按照具体的项目需求更改部分内容.需要注意的是,本项目是以使用注解为前提完成的项目. 一.项目目录 首先说明一下,本项目是maven项目,采用Spring+Springmvc+Hibernate的框架,前端模板引擎采用thymeleaf,html代码存放在下面结构的templat

mybatis springmvc velocity环境搭建

前言 轻量级ORM框架MyBatis完美的配合SpringMVC web框架实现了后台action的开发,结合Java模版引擎velocity实现了Java代码与前端代码的隔离. 搭建过程 后台配置mybatis 添加依赖 Spring 3.2.4-RELEASE <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-web

SpringMVC的环境搭建

MyBatis框架-->持久层框架-->Object[对象]Relation[关系型数据库]Mapping[在MyBatis的体现是哪个映射文件中国的<resultMap>标签]框架  对JDBC的封装,需要定制SQL语句.处理的是DataAccessOBject数据访问对象 SpringMVC它隶属于Spring框架,它只是Spring这个庞大框架下的一个小模块,SpringMVC是替换我们之前学过的Servlet. 首先如何学习一个框架? 1.找jar 2.配置核心配置文件 3

SpringMVC工作环境搭建 配置文件

web.xml配置 在服务器端容器启动之前加载配置文件的顺序:context-param>listener>filter>servlet //容器配置application上下文的时候加载的文件 //这是spring容器的配置文件 <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.