SpringMVC + freemarker instance based on Annotation

Source: Internet
Author: User

SpringMVC + freemarker instance based on Annotation

SpringMVC + freemarker demo instance based on Annotation

Web project diagram



Web. xml file

Xml Code
  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 files

    Xml Code
    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 class

      Java code
      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 class

        Java code
        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 File

          Ftl code
          1. Insert title here
          2. <# List users as user>
          3. Username: $ {user. username}
          4. Password: $ {user. password}
          5. Deploy to tomcat and run: http: // localhost: 8080/springmvc/home/index

            Display result:

            Username: zhangsan
            Password: 1234

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.