springboot整合jsp thymeleaf,实现小程序跳转

老树开新花,也不是那么容易,在网上调研了一番,但用到自己的项目中各种错,可能网上只是简单的实例,而我的是真实的工程化项目。
16年有个老系统使用的springmvc,现在想把他改造成springboot,但我又不想改造页面,于是就思考是否springboot是否可以平滑切换过来呢。
springboot中使用jsp
SpringBoot整合JSP超详细步骤
1 yml配置

spring:
  mvc:
    view:
      prefix: /
      suffix: .jsp
    static-path-pattern: /resources/**

下面的配置没有生效,springboot版本为2.3.12.RELEASE

spring:
  mvc:
    view:
      prefix: /WEB-INF/view/
      suffix: .jsp

2 jsp和静态文件结构
静态文件(css、js等)在server所在的工程,不能放在组件中,尝试是失败的
1
jsp页面放在组件中
2
3
在这里插入图片描述

3 spring boot配置

<jasper.version>10.0.8</jasper.version>
        <jstl.version>1.2</jstl.version>
 <!-- servlet依赖 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>
        <!-- tomcat的支持 -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>${jasper.version}</version>
        </dependency>
        <!-- SpringBoot 核心组件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>

<resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <!-- 源文件夹 -->
                <directory>src/main/webapp</directory>
                <!-- 指定编译到的路径为 META-INF/resources -->
                <targetPath>META-INF/resources</targetPath>
                <!-- 指定源文件夹中的哪些资源需要进行编译 -->
                <includes>
                    <include>*.*</include>
                </includes>
            </resource>
        </resources>

4 controller
使用@Controller,而不是@RestController
一年后再次捡起,在新的工程中做转换,发现上述的方案居然行不通,莫非我当时写漏了,提示错误

16:39:22.598 WARN  org.springframework.web.servlet.PageNotFound - No mapping for GET /kka.jsp

整合jsp使用起来总归有些不顺心的地方,老实使用官方推荐的thymeleaf,也不是不行。但是稍有不正确,也会有页面不出来的时候.

20:24:14.957 ERROR org.thymeleaf.TemplateEngine - [THYMELEAF][http-nio-0.0.0.0-8352-exec-1] Exception processing template "kka": Error resolving template [kka], template might not exist or might not be accessible by any of the configured Template Resolvers
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [kka], template might not exist or might not be accessible by any of the configured Template Resolvers
	at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869)
	at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607)
	at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098)
	at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072)
	at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:366)
	at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:190)
	at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1373)
	at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1118)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1057)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:626)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)

还得靠自己来实现

<!--Thymeleaf模板依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
spring:
  thymeleaf:
    cache: false
    prefix: classpath:/views/
    mode: HTML5
    suffix: .html
    encoding: UTF-8
    servlet:
      content-type: text/html

将我们的小程序提供给第三方微信公众号,用户关注第三方微信公众号的时候,打开我们的小程序.
关注微信公众号会发送一条消息:

String url="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid
                                            + "&redirect_uri="+ URLEncoder.encode(backUrl)
                                            + "&response_type=code"
                                            + "&scope=snsapi_userinfo"
                                            + "&state=STATE#wechat_redirect";
                                    message = eaycScanRegister(provinceType,fromUserName,eventKey);
                                    if(StringUtil.isNotEmpty(message) && "扫码登录成功".equals(message)){
                                        message = "扫码登录注册成功!" + "<a  href='"+url+"'>点击授权昵称头像信息</a>";
                                    }

如果我们提供一个backUrl给对方,比他们照着微信跳转 小程序官网要好,因为是否调换小程序,还是打开h5页面,里面的功能是我们的,是否有兼容性问题,也应该由我们来处理.

本要求,以及最低的系统版本要求。

微信版本要求为:7.0.12及以上
系统版本要求为:iOS 10.3及以上、Android 5.0及以上

搞技术的人就是灵活,例如公众号h5网页跳转小程序,他们就想到了先打开一个h5页面,然后再这个h5页面再跳转到小程序,问题就解决了.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实现jsp页面间的跳转 public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {   response.setContentType("text/html; charset=gb2312");   ServletContext sc = getServletContext();   RequestDispatcher rd = null;   rd = sc.getRequestDispatcher("/index.jsp"); //定向的页面   rd.forward(request, response); }   通常在servlet中使用,不在jsp中使用。    2.response.sendRedirect()   是在用户的浏览器端工作,sendRedirect()可以带参数传递,比如servlet?name=frank传至下个页面,同时它可以重定向至不同的主机上,sendRedirect()可以重定向有frame.的jsp文件.   重定向后在浏览器地址栏上会出现重定向页面的URL   例:在servlet中重定向 public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {   response.setContentType("text/html; charset=gb2312");   response.sendRedirect("/index.jsp"); }   由于response是jsp页面中的隐含对象,故在jsp页面中可以用response.sendRedirect()直接实现重定位。   注意:    (1) 使用response.sendRedirect时,前面不能有HTML输出   这并不是绝对的,不能有HTML输出其实是指不能有HTML被送到了浏览器。事实上现在的server都有cache机制,一般在8K(我是说 JSP SERVER),这就意味着,除非你关闭了cache,或者你使用了out.flush()强制刷新,那么在使用sendRedirect之前,有少量的HTML输出也是允许的。    (2) response.sendRedirect之后,应该紧跟一句return;   我们已经知道response.sendRedirect是通过浏览器来做转向的,所以只有在页面处理完成后,才会有实际的动作。既然你已经要做转向了,那么后的输出还有什么意义呢?而且有可能会因为后面的输出导致转向失败。   比较:    (1) Dispatcher.forward()是容器中控制权的转向,在客户端浏览器地址栏中不会显示出转向后的地址;    (2) response.sendRedirect()则是完全的跳转,浏览器将会得到跳转的地址,并重新发送请求链接。这样,从浏览器的地址栏中可以看到跳转后的链接地址。   前者更加高效,在前者可以满足需要时,尽量使用RequestDispatcher.forward()方法.   注:在有些情况下,比如,需要跳转到一个其它服务器上的资源,则必须使用HttpServletResponse.sendRequest()方法。    3.   它的底层部分是由RequestDispatcher来实现的,因此它带有RequestDispatcher.forward()方法的印记。   如果在之前有很多输出,前面的输出已使缓冲区满,将自动输出到客户端,那么该语句将不起作用,这一点应该特别注意。   另外要注意:它不能改变浏览器地址,刷新的话会导致重复提交    4.修改HTTP header的Location属性来重定向   通过设置直接修改地址栏来实现页面的重定向。    jsp文件代码如下:    5.JSP实现在某页面停留若干秒后,自动重定向到另一页面   在html文件中,下面的代码: <meta http-e
Spring Boot是一个开源的Java开发框架,用于快速构建基于Spring的应用程序。Thymeleaf是一个Java的服务器端模板引擎,可以与Spring Boot框架很好地集成。 要实现HTML页面之间的跳转,可以使用Thymeleaf的语法结合Spring Boot的控制器来完成。 首先,需要在pom.xml文件中添加Thymeleaf的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 接下来,在Spring Boot的控制器中添加一个处理请求的方法,该方法返回要跳转HTML页面的名称: ```java @Controller public class MyController { @RequestMapping("/hello") public String hello() { return "hello"; // 这里返回的是要跳转HTML页面的名称,不需要后缀名 } } ``` 然后,在resources/templates目录下创建名为hello.htmlHTML页面,用于展示hello页面的内容: ```html <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Hello</title> </head> <body> <h1>Hello, Thymeleaf!</h1> </body> </html> ``` 最后,启动Spring Boot应用程序,在浏览器中访问http://localhost:8080/hello,即可看到跳转到hello.html页面,并显示"Hello, Thymeleaf!"的信息。 以上就是使用Spring BootThymeleaf实现HTML页面跳转的简单示例。通过Thymeleaf的语法和Spring Boot的控制器,我们可以方便地实现页面之间的跳转和数据的渲染。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

warrah

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值