文章目录
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
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!--配置应如上配置-->
<!-- 配置SpringMVC -->
<!-- 1.开启SpringMVC注解模式 -->
<!--可以添加一些默认的convert-->
<!-- 简化配置:
(1)自动注册DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter
(2)提供一些列:数据绑定,数字和日期的format @NumberFormat, @DateTimeFormat, xml,json默认读写支持
-->
<mvc:annotation-driven />
<!-- 2.静态资源默认servlet配置
(1)加入对静态资源的处理:js,gif,png
(2)允许使用"/"做整体映射
-->
<mvc:default-servlet-handler/>
<!-- <bean id="simpleUrlHandleMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<!– /hello 路径的请求交给 id为 helloController的控制器处理–>
<prop key="/hello">helloController</prop>
</props>
</property>
</bean>
<bean id="helloController" class="javaweb.com.controller.HelloController"></bean>-->
<!--扫描controller下的组件-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp"/>
</bean>
<!--for example: "myView" -> "/WEB-INF/jsp/myView.jsp"-->
<context:component-scan base-package="javaweb.com.controller"/><!--双引号里面填XXXController Java文件所在的目录-->
</beans>
freemarker(视图模板):
工程springMVCFreeMarkerDemo
依赖项:
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</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);
}
}