Restful 风格开发 && Spring MVC 的视图解析器---使用 beetl 模板引擎

一、restful 风格

restful 的目的
将用户的行为当成是对数据库中记录的操作:

增加用户:/user post(post方式)
删除用户:/user/2 delete(删除第2条记录)
修改用户:/user/2 put
查询用户:/user (或根据id查:/user/2)

示例:
1)将CommonResponse加进来:

package net.xikee.druid;

import java.io.Serializable;

public class CommonResponse implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 表示当前相应的状态是成功或者失败
	 */
	private Boolean status;
	/**
	 * 表示当响应失败之后给前端的错误提示
	 */
	private String msg;
	/**
	 * 表示当响应成功之后返回给前端的数据
	 */
	private Object data;
	
	
	public static CommonResponse isOk(Object data) {
		return new CommonResponse(true, "请求成功", data);
	}
	
	public static CommonResponse error(String msg) {
		return new CommonResponse(false, msg, null);
	}
	
	public CommonResponse() {
		super();
		// TODO Auto-generated constructor stub
	}
	public CommonResponse(Boolean status, String msg, Object data) {
		super();
		this.status = status;
		this.msg = msg;
		this.data = data;
	}
	public Boolean getStatus() {
		return status;
	}
	public void setStatus(Boolean status) {
		this.status = status;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public Object getData() {
		return data;
	}
	public void setData(Object data) {
		this.data = data;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
}

2)UserController类

a. 前端传递json数据,后台接收时对象要使用@RequestBody

b. @RestController 有controller的功能,除此之外相当于在该类中的每一个方法上面都加上了@responsebody post get

c. @PostMapping 相当于:@RequestMapping(method=RequestMethod.POST)

d. 加上@RequestBody 这个注解,spring MVC会将 json 格式转化回对象类型,跟@ResponseBody刚好相反

package net.xikee.druid;

import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import net.xikee.druid.pojo.User;
/**
 * Restful风格开发
 * @author Administrator
 *   put delete /user/regist /user/login 将用户的行为,当成是对数据库中记录的操作
 */
 
@RestController
@RequestMapping("user")
public class UserController {

	@PostMapping	//相当于:@RequestMapping(method=RequestMethod.POST)
	public CommonResponse addUser( @RequestBody User user) {
	 //加上@Request这个注解,spring MVC会将 json 格式转化回对象类型,跟@ResponseBody刚好相反
		System.out.println("addUser");
		return CommonResponse.isOk("SUCCESS");
	}
	
	//用正则表达式检测传入的是否为数字(\d+),如果不符,spring将请求挡回去
	@DeleteMapping("/{id:\\d+}")	//这里的{id}相当于一个占位符(相当数据库的"?")
	public CommonResponse deleteUser(@PathVariable int id) {//如果占位符跟参数名字匹配,则从访问的路径中把值赋进去
		System.out.println("deleteUser");
		return CommonResponse.isOk("SUCCESS");
	}
	
	@PutMapping("/{id:\\d+}")
	public CommonResponse updateUser(@PathVariable int id) {
		System.out.println("updateUser");
		return CommonResponse.isOk("SUCCESS");
	}
	@GetMapping("/{id:\\d+}")
	public CommonResponse getUserById(@PathVariable int id) {
		System.out.println("getUser");
		return CommonResponse.isOk("SUCCESS");
	}
}

由于没有写form提交表单,这里使用 rested 插件模拟 post 和 delete 等请求:
在这里插入图片描述

二、spring MVC 的视图解析器

web 端,服务器能控制web页面跳转,但是在 app 端,服务器无法控制其跳转(只能返回成功或失败,让移动端自己判断)
一般我们都是以 ModelAndView 返回

通过 Spring MVC 视图解析器,访问资源:WEB-INF / jsp / index.jsp

1.JSP模板引擎
<!-- 视图解析器 对返回的视图名做批量操作 用来解析jsp 跳转到index.jsp,只需写index即可 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewNames">
			<value>jsp/**</value>
		</property>
		<!-- 对返回的视图名加上前缀和后缀 -->
		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

在Controller层使用:

@RestController
@RequestMapping("hello")
public class HelloController {
	
	@RequestMapping("2")
	public ModelAndView sayHello(@RequestParam(name="name",defaultValue="", required=true) String username, String password, User user) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("index");
		mv.addObject("username", "xikee");
		return mv;
	}
}
2.beetl 模板引擎

1)引入 beetl 核心依赖:

<!-- https://mvnrepository.com/artifact/com.ibeetl/beetl -->
		<dependency>
			<groupId>com.ibeetl</groupId>
			<artifactId>beetl</artifactId>
			<version>3.0.10.RELEASE</version>
		</dependency>

2)在 applicationContext-mvc.xml 的MVC配置文件中注册bean
在这里插入图片描述
在这里插入图片描述
BeetlSpringViewResolver 这个 bean 会去读取 spring-druid/src/main/resources 下的beetl.properties文件

<!-- 读取beetl的配置文件 -->
	<bean id="beetlConfig" init-method="init"
		class="org.beetl.ext.spring.BeetlGroupUtilConfiguration">
	</bean>

	<bean id="viewResolver"
		class="org.beetl.ext.spring.BeetlSpringViewResolver">
		<property name="contentType" value="text/html;charset=UTF-8"></property>
		</property>
		<property name="prefix" value="/WEB-INF/beetl/"></property>
		<property name="suffix" value=".html"></property>
	</bean>

3)设置模板引擎占位符(比如 jsp 的占位符为<% %>表示要开始写jsp代码)
在这里插入图片描述这里我们将 beetl 占位符设为:

DELIMITER_STATEMENT_START=<!--:
  DELIMITER_STATEMENT_END=-->	//占位符则为  <!--:   -->

在 beetl 中编辑,网页访问结果为:xikee 14
在这里插入图片描述beetl 语法参考:http://ibeetl.com/beetlonline

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值