基于注解SpringMVC+freemarker实例

基于注解的SpringMVC+freemarker demo实例

 

web项目图



 

web.xml文件

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app id="WebApp_ID" 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">  
  3.   <servlet>  
  4.     <!-- 配置DispatcherServlet -->  
  5.     <servlet-name>springmvc</servlet-name>  
  6.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  7.     <!-- 指定spring mvc配置文件位置 不指定使用默认情况 -->  
  8.       <init-param>     
  9.         <param-name>contextConfigLocation</param-name>     
  10.         <param-value>/WEB-INF/springmvc-servlet.xml</param-value>  
  11.         <!--默认:/WEB-INF/<servlet-name>-servlet.xml  
  12.         classpath方式:<param-value>classpath:/spring-xml/*.xml</param-value>      
  13.          -->     
  14.        </init-param>  
  15.     <!-- 设置启动顺序 -->  
  16.     <load-on-startup>1</load-on-startup>  
  17.   </servlet>  
  18.   <!-- 配置映射 servlet-name和DispatcherServlet的servlet一致 -->  
  19.   <servlet-mapping>  
  20.     <servlet-name>springmvc</servlet-name>  
  21.     <url-pattern>/</url-pattern><!-- 拦截以/所有请求 -->  
  22.   </servlet-mapping>  
  23.   <welcome-file-list>  
  24.     <welcome-file>index.jsp</welcome-file>  
  25.   </welcome-file-list>  
  26. </web-app>  

 

springmvc-servlet.xml文件

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.     http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.      http://www.springframework.org/schema/mvc   
  8.      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd   
  9.      http://www.springframework.org/schema/aop   
  10.      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
  11.      http://www.springframework.org/schema/context   
  12.      http://www.springframework.org/schema/context/spring-context.xsd">  
  13.   
  14.      <!-- 默认注解映射支持 -->  
  15.      <mvc:annotation-driven/>   
  16.      <!-- 自动扫描包 -->   
  17.      <context:component-scan base-package="com.spring.freemarker" />  
  18.        
  19.      <!--<context:annotation-config /> 配置自动扫描包配置此配置可省略-->     
  20.      <!--<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" 配置自动扫描包配置此配置可省略/>-->  
  21.      <!-- 配置freeMarker的模板路径 -->  
  22.      <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
  23.         <property name="templateLoaderPath" value="WEB-INF/ftl/" />  
  24.         <property name="defaultEncoding" value="UTF-8" />  
  25.      </bean>  
  26.      <!-- freemarker视图解析器 -->  
  27.      <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  
  28.         <property name="suffix" value=".ftl" />  
  29.         <property name="contentType" value="text/html;charset=UTF-8" />  
  30.         <!-- 此变量值为pageContext.request, 页面使用方法:rc.contextPath -->  
  31.         <property name="requestContextAttribute" value="rc" />  
  32.      </bean>  
  33. </beans>  

 

 FreeMarkerController类

Java代码   收藏代码
  1. package com.spring.freemarker;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8.   
  9. import org.springframework.stereotype.Controller;  
  10. import org.springframework.web.bind.annotation.RequestMapping;  
  11. import org.springframework.web.servlet.ModelAndView;  
  12.   
  13. import com.spring.vo.User;  
  14.   
  15. @Controller  
  16. @RequestMapping("/home")  
  17. public class FreeMarkerController {  
  18.   
  19.     @RequestMapping("/index")  
  20.     public ModelAndView Add(HttpServletRequest request, HttpServletResponse response) {  
  21.   
  22.         User user = new User();  
  23.         user.setUsername("zhangsan");  
  24.         user.setPassword("1234");  
  25.         List<User> users = new ArrayList<User>();  
  26.         users.add(user);  
  27.         return new ModelAndView("index""users", users);  
  28.     }  
  29.   
  30. }  

 User类

Java代码   收藏代码
  1. package com.spring.vo;  
  2.   
  3. public class User {  
  4.   
  5.     private String username;  
  6.     private String password;  
  7.   
  8.     public String getUsername() {  
  9.         return username;  
  10.     }  
  11.   
  12.     public void setUsername(String username) {  
  13.         this.username = username;  
  14.     }  
  15.   
  16.     public String getPassword() {  
  17.         return password;  
  18.     }  
  19.   
  20.     public void setPassword(String password) {  
  21.         this.password = password;  
  22.     }  
  23.   
  24. }  

 

 index.ftl文件

Ftl代码   收藏代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5. <title>Insert title here</title>  
  6. </head>  
  7. <body>  
  8. <#list users as user>  
  9. username : ${user.username}<br/>  
  10. password : ${user.password}  
  11. </#list>  
  12. </body>  
  13. </html>  

 部署到tomcat,运行:http://localhost:8080/springmvc/home/index

  显示结果:

  username : zhangsan
  password : 1234

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值