显示如下的界面:
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。
比如:需要用户绑定账号到微信公众号,来实现发送模板消息。