spring关于使用MVC框架的配置
首先要配置 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- Spring Framework -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml</param-value>
</context-param>
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
</web-app>
然后配置 spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="my" />
<mvc:annotation-driven />
<!-- 添加 FreeMarker 支持 -->
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<!-- 此处为模板路径 -->
<property name="templateLoaderPath" value="/WEB-INF/template" />
<property name="defaultEncoding" value="utf-8"></property>
</bean>
<bean id="freeMarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="suffix" value=".html"></property>
<property name="contentType" value="text/html;charset=utf-8"></property>
<!-- 添加 base相对路径 requestContextAttribute 属性 -->
<property name="requestContextAttribute" value="RC"/>
</bean>
</beans>
然后要有jar包支持
链接:https://pan.baidu.com/s/1I2b2A5119Agw2Jw9aSafBg
提取码:96ch
使用方法
模板自己配置,这里只显示模板路径
package my;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/student")
public class StudentController
{
//MVC
@GetMapping("/query")
public String query(Model model, int from, int to)
{
List<Student> studentList = new ArrayList<>();
for(Student s : DemoDB.list)
{
if(s.getId() >= from && s.getId() <= to)
{
studentList.add(s);
}
}
model.addAttribute("studentList", studentList);
return "query"; // 模板路径 /WEB-INF/template/query.html
}
//REST接口
@GetMapping("/query2")
public ResponseEntity<String> query2( @RequestParam("from") Integer n1
, @RequestParam("to") Integer n2)
{
List<Student> result = new ArrayList<>();
for(Student s : DemoDB.list)
{
if(s.getId() >= n1 && s.getId() <= n2)
{
result.add( s );
}
}
// 构造一个JSON对象 (不使用Jackson JSON,用你自己熟悉的JSON库 )
JSONArray jresp = new JSONArray( result);
// 构造 ResponseEntity 对象返回
return ResponseEntity.ok()
.contentType(MediaType.APPLICATION_JSON_UTF8)
.body(jresp.toString(2));
}
//显示学生信息
@GetMapping("/{id}/info")
public Object get(Model model, @PathVariable int id)
{
Student stu = null;
for(Student s : DemoDB.list)
{
if(s.getId() == id)
{
stu = s;
break;
}
}
//如果找不到,则显示错误
if(stu == null)
{
model.addAttribute("errMsg", "找不到记录, id=" + id);
return "error_404";
// return "redirect:/a/some.html"; //重定向,我这里有模板
// return ResponseEntity.status(404).build();
}
model.addAttribute("stu", stu);
return "info";
}
}
这里只贴出 query.html 模板
<!DOCTYPE html>
<html>
<head>
<title> 查询结果 </title>
<meta charset='utf-8'>
<style>
/* 表格样式 */
table{
border-collapse: collapse; /* 边线收缩 */
table-layout: fixed ; /* 自动布局还是固定宽度 */
word-break: break-all; /* 换行设定 */
word-wrap:break-word; /* 换行折断设定 */
}
/* 逗号表示同时指定多个目标的样式 */
table,td,th{
border: 1px solid #888;
padding: 4px;
}
.row{
margin: 10px 0;
}
</style>
</head>
<body>
<div style='width:500px;margin:40px auto auto auto' >
<div class='row'> 查询结果 </div>
<div>${Session.user !"未登录"}</div>
<table>
<tr>
<th style='width:80px'> 学号 </th>
<th style='width:120px'> 姓名 </th>
<th style='width:60px'> 性别 </th>
<th> 手机号 </th>
</tr>
<tr>
<#list studentList as stu>
<tr>
<td> ${stu.id?c} </td>
<td> ${stu.name}</td>
<td> ${stu.sex?c}</td>
<td> ${stu.cellphone}</td>
</tr>
</#list>
</table>
</div>
</body>
</html>
本文仅供自我参考,侵权立删