SpringMVC和freemark笔记

文章目录

    • springMVC
      • 使用MVC的注释功能:
        • HelloControllorDemo.java
        • 配置文件:dispatcher-servlet.xml
      • freemarker(视图模板):
        • 依赖项:
        • 模板文件,user.ftl
        • 测试文件:TestFreeMarker

springMVC

仅当个人笔记使用
工程名:mySpringMVC

使用MVC的注释功能:

HelloControllorDemo.java

package javaweb.com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.io.Writer;

@Controller
public class HelloControllerDemo {

    //直接通过注释获得响应的内容,不用一步一步的从httprequest中获取
    //浏览器输入Localhost:8080/hello/123?msg=hello
    @RequestMapping("hello/{userId}")
    public void spring(@PathVariable("userId") String userId, @RequestParam("msg") String msg, @RequestHeader String host, Writer writer) throws IOException {
        writer.write("userId="+userId+"   msg="+msg+"  host="+host);
    }
}

配置文件:dispatcher-servlet.xml


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

    
    
    
    
    <mvc:annotation-driven />

    
    <mvc:default-servlet-handler/>

    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/page/" />
        <property name="suffix" value=".jsp"/>
    bean>
    
    <context:component-scan base-package="javaweb.com.controller"/>
beans>

freemarker(视图模板):

工程springMVCFreeMarkerDemo

依赖项:

<dependency>
  <groupId>org.freemarkergroupId>
  <artifactId>freemarkerartifactId>
  <version>2.3.23version>
dependency>

模板文件,user.ftl

<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>用户信息title>
head>
<body>
<p>Name:${name}p>
<p>Password:${password}p>
body>
html>

测试文件:TestFreeMarker

package main.webapp.com;
//测试,freemarker的基本使用
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

public class TestFreeMarker {
    public static void main(String[] args) throws IOException, TemplateException {
        //获取configuration对象
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);

        //设置模板加载的绝对路径,将会识别出此目录下的模板
        configuration.setDirectoryForTemplateLoading(new File("E:\\myProject\\springMVCFreeMarkerDemo\\src\\main\\webapp\\resources"));
        //设置默认字符集
        configuration.setDefaultEncoding("UTF-8");
        //设置模板异常处理模式,rethrow_handler
        configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

        //获取模板对象,传入已经定义好的基本模板文件user.ftl
        Template template = configuration.getTemplate("user.ftl");

        //创建用户root的map集合,用来存放用户的用户名和密码
        Map<String,Object> root = new HashMap<String,Object>();
        root.put("name","zhd");
        root.put("password","123");

        //创建Writer对象,用来设定,最终模板在哪显示
        Writer writer = new OutputStreamWriter(System.out);//显示在控制台中
        //处理模板
        template.process(root,writer);
    }
}

你可能感兴趣的:(spring)