基于注解SpringMVC+freemarker实例

2015-01-24 05:33:40 · 作者: · 浏览: 5

基于注解的SpringMVC+freemarker demo实例

web项目图


\

web.xml文件

Xml代码 收藏代码
  1. springmvc
  2. org.springframework.web.servlet.DispatcherServlet
  3. contextConfigLocation
  4. /WEB-INF/springmvc-servlet.xml
  5. 1
  6. springmvc
  7. /
  8. index. jsp
  9. springmvc-servlet.xml文件

    Xml代码 收藏代码
    1. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    2. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    3. xsi:schemaLocation="http://www.springframework.org/schema/beans
    4. http://www.springframework.org/schema/beans/spring-beans.xsd
    5. http://www.springframework.org/schema/mvc
    6. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    7. http://www.springframework.org/schema/aop
    8. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    9. http://www.springframework.org/schema/context
    10. http://www.springframework.org/schema/context/spring-context.xsd">
    11. FreeMarkerController类

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

        User类

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

          index.ftl文件

          Ftl代码 收藏代码
          1. html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
          2. Insert title here
          3. <#list users as user>
          4. username : ${user.username}
          5. password : ${user.password}
          6. 部署到tomcat,运行:http://localhost:8080/springmvc/home/index

            显示结果:

            username : zhangsan
            password : 1234