访问spring boot 接口返回一个view,显示相关提示信息

显示如下的界面:

1、方法一:通过@ResponseBody,直接返回字符串html,并显示到view。

@Controller
@RequestMapping("wx")
public class WxTxController extends BaseController {
    

    /**
	 * 方法1:通过@ResponseBody,直接返回字符串。
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(path = "/result", method = RequestMethod.GET)
	public String IndexPage0(HttpServletRequest req) {
        String msg = "成功授权啦!!!";
        return "<h3>" + msg + "<h3>";
    }

    
}

说明:这个方法不需要额外的html文件,直接返回一个String的html到view。

2、方法二:ModelAndView返回,指定html名用setViewName方法。

@Controller
@RequestMapping("wx")
public class WxTxController extends BaseController {
    

    /**
	 * 方法2:通过ModelAndView,返回到html中,来显示view。
	 * 
	 * @return
	 */
	@RequestMapping(path = "/result2", method = RequestMethod.GET)
	public ModelAndView IndexPage2(HttpServletRequest req) {
		ModelAndView modelView = new ModelAndView();
		modelView.setViewName("checkResult");
		modelView.addObject("msg", "成功授权啦!!!");
		return modelView;
	}

    
}

而addObject方法,来设置返回参数的值,这个值可以是处理以后返回的,目前只是测试。

注意:保证 /src/main/resources/templates/下面有这个checkResult.html。

方法三:通过Model来处理参数,并return返回html。

@Controller
@RequestMapping("wx")
public class WxTxController extends BaseController {
    

    /**
	 * 方法3:通过Model,返回到html中,来显示view。
	 * 
	 * @return
	 */
	@RequestMapping(path = "/result2", method = RequestMethod.GET)
	public String IndexPage2(Model model,HttpServletRequest req) {
		model.addAttribute("msg",  "成功授权啦!!!");
		return "checkResult";
	}

    
}

注意:保证 /src/main/resources/templates/下面有这个checkResult.html。

pom.xml

<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

这样我们就可以实现访问接口,并且可以进行相关业务处理后,再显示给UI。

比如:需要用户绑定账号到微信公众号,来实现发送模板消息。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值