`

spring MVC

阅读更多
Spring MVC & Freemarker
1.Reference:
spring-framework-2.5.6/docs/MVC-step-by-step/html_single/index.html

2.config relative : web.xml
<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >

  <servlet>
    <servlet-name>springapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>springapp</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>
      index.jsp
    </welcome-file>
  </welcome-file-list>

</web-app>

Spring configuration:
/WEB-INF/springapp-servlet.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

  <!-- the application context definition for the springapp DispatcherServlet -->

  <bean name="/hello.htm" class="springapp.web.HelloController"/>

</beans>


3.Spring C –controller
import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.sun.org.apache.commons.logging.Log;
import com.sun.org.apache.commons.logging.LogFactory;

public class HelloController implements Controller {

    protected final Log logger = LogFactory.getLog(getClass());

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    String now = (new Date()).toString();
        logger.info("Returning hello view with " + now);
        return new ModelAndView("hello.jsp", "now", now);
    }
}

ModelAndView :hello.jsp redirect page ,now :parameter dispatcher to the page;


<bean name="/hello.htm" class="springapp.web.HelloController"/>
It means the url end with “hello.htm” will be process by the controller



4.The project content



Spring MVC base on Freemarker
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!--
the application context definition for the springapp DispatcherServlet
-->

<bean name="/hello.htm" class="com.test.action.HelloController" />

<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/ftl">
</property>
<property name="defaultEncoding" value="UTF-8"></property>
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">10</prop>
<prop key="locale">zh_CN</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="contentType" value="text/html;charset=UTF-8"></property>
<property name="suffix">
<value>.ftl</value>
</property>
</bean>
</beans>

In the controller:
return new ModelAndView("hello", "now", now);
it will turn to the page “hello.ftl”
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    精通Spring MVC 4

    Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。Spring MVC4是当前zuixin的版本,在众多特性上有了进一步的提升。, 在精通Spring...

    Spring MVC 入门实例

    首先, 我需要在你心里建立起 Spring MVC 的基本概念. 基于 Spring 的 Web 应用程序接收到 http://localhost:8080/hello.do(事实上请求路径是 /hello.do) 的请求后, Spring 将这个请求交给一个名为 helloController ...

    Spring MVC+MyBatis开发从入门到项目实战

    《Spring MVC+MyBatis开发从入门到项目实战》分为4篇。第1篇是Java开发环境的搭建,包括JDK的下载与安装、环境变量的配置、MyEclipse的下载与基本配置。第2篇是MyBatis技术入门,包括剖析JDBC的弊端、MyBatis的背景...

    精通Spring MVC 4 中文

    精通Spring MVC 4 中文

    [免费]Spring MVC学习指南(高清)

    Spring MVC是Spring框架中用于Web应用快速开发的一个模块,其中的MVC是Model-View-Controller的缩写。作为当今业界最主流的Web开发框架,Spring MVC已经成为当前最热门的开发技能,同时也广泛用于桌面开发领域。 ...

    Spring MVC

    Spring MVC 是Spring框架最重要的的模块之一。它以强大的Spring IoC容器为基础,并充分利用容器的特性来简化它的配置。 commons-logging-1.2.jar jackson-annotations-2.6.6.jar jackson-core-2.6.6.jar jackson-...

    大优惠 Spring MVC学习指南(第2版)2017.pdf

    Spring MVC是Spring框架中用于Web应用快速开发的一个模块,其中的MVC是Model-View-Controller的缩写。作为当今业界最主流的Web开发框架,Spring MVC已经成为当前最热门的开发技能,同时也广泛用于桌面开发领域。 ...

    SSM(spring+spring MVC+mybatis)开发学生信息后台管理系统

    SSM(spring+spring MVC+mybatis)开发学生信息后台管理系统,实现学生增删改查功能设计一个简单的学生信息管理系统,要求使用SSM框架技术整合实现,用户登录后能够通过Web页面添加、删除、修改和查询学生信息 ...

    spring mvc jar包

    spring mvc 开发jar包

    Spring MVC所需jar包

    Spring MVC所需jar包,包含java开发中 Spring MVC架构中最常用的jar包

    spring MVC环境搭建 所需jar包.zip

    spring MVC环境搭建 所需jar包 ├── commons-logging-1.1.1.jar ├── jstl.jar ├── spring-aop-4.3.0.RELEASE.jar ├── spring-aspects-4.3.0.RELEASE.jar ├── spring-beans-4.3.0.RELEASE.jar ├...

    Servlet JSP和Spring MVC初学指南

    Servlet JSP和Spring MVC初学指南

    spring MVC数据绑定大全

    spring MVC数据绑定 含例子 转载自疯芒毕露的专栏 刚开始用spring mvc 做web开发时 经常会不知道如何合适绑定页面数据 用惯struts2的朋友更认为spring mvc 绑定数据不如struts2方便 本人最开始也是这么认为 经过一段...

    spring mvc

    spring mvc最小代码。spring mvc最小代码。spring mvc最小代码。spring mvc最小代码。

    Spring MVC, A Tutorial, second edition 【2016】

    Spring MVC: A Tutorial (Second Edition) by Paul Deck AZW3/MOBI/EPUB/PDF 多种版本 This is a tutorial on Spring MVC, a module in the Spring Framework for rapidly developing web applications. The MVC in...

    Spring+Spring mvc+Hibernate+Bootstrap、企业级员工信息管理系统

    01. 采用后台及前台的 Spring + Spring mvc + Hibernate + Bootstrap 02. 后台全注解式的开发(除了必要的spring和hibernate的xml配置以外) 03. 后台通过自定义注解结合一个访问拦截器实现整个系统的权限控制 04...

    Spring MVC + Mybatis+Spring实现的个人博客系统

    Spring MVC + Mybatis+Spring实现的个人博客系统基于SSM实现的个人博客系统.zip

    spring mvc框架依赖全面jar

    spring mvc轻量级框架搭建,依赖全面jar文件包。下载解压直接将jar文件复制到工程中的lib中。

Global site tag (gtag.js) - Google Analytics