因为需要MockHttpServletResponse对象来得到输出的内容,要引入的包

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>

注入ViewResolver解析页面

    @Autowired
    private InternalResourceViewResolver viewResolver;

使用过程

@GetMapping("test")
    public JsonResult test(HttpServletRequest request,
                           HttpServletResponse response)throws Exception{
        ModelMap map = new ModelMap();
        map.put("param_json", "asdasd");
        map.put("loginuser", null);
        View resolve = viewResolver.resolveViewName("design", Locale.CHINA);
        MockHttpServletResponse mockResp = new MockHttpServletResponse();
        resolve.render(map, request, mockResp);
        System.out.println("rendered html : " + mockResp.getContentAsString());
        return JsonResult.ok(mockResp.getContentAsString(), null);
    }

查看打印和返回的结果,发现生成了html

springmvc手动渲染jsp

 

springmvc手动渲染jsp

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-23
  • 2021-09-16
  • 2021-12-21
  • 2022-12-23
  • 2021-11-17
  • 2021-09-01
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-22
  • 2022-12-23
  • 2021-06-02
  • 2021-07-27
  • 2022-12-23
相关资源
相似解决方案