springboot整合jsp,实现公交车站路线图

 更新时间:2021年01月03日 12:27:12   作者:程序帮  
这篇文章主要介绍了springboot整合jsp,实现公交车站路线图的步骤,帮助大家更好的理解和使用springboot框架,感兴趣的朋友可以了解下

开发环境:

  1. jdk 8
  2. intellij idea
  3. tomcat 8
  4. mysql 5.7
  5. maven 3.6

所用技术:

  • springboot
  • jsp
  • 数据静态初始化

项目介绍

使用springboot整合jsp,在后端写入公交路线名称和详细站点,前端页面可条件查询具体的内容,如公交路线,公交名称,车俩信息等。

运行效果

前台用户端:

  • 路线选择

  • 路线详情

数据准备:

BusData.txt

准备工作:

pom.xml加入jsp模板引擎支持:

<dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-jasper</artifactId>
  <scope>provided</scope>
</dependency>

springboot配置jsp

spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

重要代码:

bus数据初始化

@PostConstruct
private void initBusData(){
  try{
    File file = new File(BusMap.getClass().getResource("/").getPath());
    FileReader fileReader = new FileReader(file.getPath()+"/static/BusData.txt","GBK"); //初始化BusData.txt 数据
    List<String> readLines = fileReader.readLines();
    for(String str:readLines){
      if(!"".equals(str)){
        String[] data=str.split("#");
        String way=data[0];     //几路线
        String location=data[1];/  /地名
        String[] locations=location.split(",");
        List<Bus> list=new ArrayList<>();
        for(int i=0;i<locations.length;i++){
          int busnum=0;
          if(i%4==0){        //随机busnum
            busnum=1;
          }if(i%5==0){
            busnum=2;
          }
          Bus bus=new Bus(locations[i],busnum);
          list.add(bus);
        }
        WayList.add(way);      //添加路线
        BusMap.put(way,list);    //添加车站
      }
    }
  }catch (Exception e){
    e.printStackTrace();
  }
}

路线查询

@RequestMapping("/way")
public String search(HttpServletRequest request,String way) {
  try {
     if(null==way||"".equalsIgnoreCase(way)){
       request.setAttribute("list", BusMap.WayList); //没有搜索默认显示所有路线
       return "way";
     }else{
       List<String> wayList=new ArrayList<>();
       //模糊查询路线
       for(String str:BusMap.WayList){
         if(str.indexOf(way)>-1){
           wayList.add(str);
         }
       }
       if(wayList.size()>0){
         request.setAttribute("list", wayList); //模糊搜索出来的路线列表
         return "way";
       }else{
         return "noView"; //没有所选路线
       }
     }
  } catch (Exception e) {
    e.printStackTrace();
  }
  return "way";
}

公交车路线站展示

@RequestMapping("/view")
public String view(HttpServletRequest request,String way) {
  try {
    List<Bus> list= BusMap.getBusMap(way);
    if(list.size()>0){
      request.setAttribute("list",list ); //获取总路线
      request.setAttribute("firstBus", list.get(0).getLocation());       //第一站
      request.setAttribute("lastBus", list.get(list.size()-1).getLocation()); //最后一站
      int size = list.size();
      size =(size-1)*99;
      request.setAttribute("size",size);
      return "view";
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
  return "noView";//没有对应公交车站
}
//前端页面数据渲染
<div class="pageContent" style="background: #eeeeee;">
  <div class="pageFormContent" layoutH="55">
    <div class="timeText">${firstBus}<----->${lastBus}
      <span>( 首/末班车时间:<span style="color: red">6:00 / 23:00</span>)</span>
    </div>
    <div class="timezone" style="margin-top: 20px">
      <c:forEach var="list" items="${list}" varStatus="s">
        <div class="time" <c:if test="${s.index!=0}"> style="top: ${s.index*100+25}px;" a="1" </c:if> ><a onclick="javascript:alert(1);">${s.index+1}</a>
          <h2>${list.location}</h2>
          <c:if test="${list.busNum>0}">
            <span class="timezone3"></span>
            <div>
              <p><span style="padding-left: 30px;">${list.busNum}辆公交</span></p>
            </div>
          </c:if>
        </div>
      </c:forEach>
    </div>
  </div>
  <div class="formBar"></div>
</div>

项目总结

  1. 项目存放路径最好不要带中文路径,否则可能存在静态busData资源初始化失败
  2. 页面时间车站路线所采用时间轴方式展示,长度动态计算,部分浏览器显示可能有点错位
  3. 其他后续迭代功能后续开发,敬请关注

以上就是springboot整合jsp,实现公交车站路线图的详细内容,更多关于springboot整合jsp的资料请关注脚本之家其它相关文章!

相关文章

  • 详解jvm双亲委派机制

    详解jvm双亲委派机制

    双亲委派机制保证了核心类的安全,确保不会被修改,也保证了不会加载到重复的字节码文件,这篇文章主要介绍了jvm双亲委派机制详解,需要的朋友可以参考下
    2022-11-11
  • 使用Java SDK实现离线签名

    使用Java SDK实现离线签名

    这篇文章主要介绍了使用Java SDK实现离线签名,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • idea之Recompile、Rebuild和Build之间的区别及说明

    idea之Recompile、Rebuild和Build之间的区别及说明

    这篇文章主要介绍了idea之Recompile、Rebuild和Build之间的区别及说明,具有很好的参考价值,希望对有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • Java中的==使用方法详解

    Java中的==使用方法详解

    这篇文章主要给大家介绍了关于Java中的==使用方法的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-09-09
  • SpringBoot集成Redis使用Cache缓存的实现方法

    SpringBoot集成Redis使用Cache缓存的实现方法

    SpringBoot通过配置RedisConfig类和使用Cache注解可以轻松集成Redis实现缓存,主要包括@EnableCaching开启缓存,自定义key生成器,改变序列化规则,以及配置RedisCacheManager,本文为使用SpringBoot与Redis处理缓存提供了详实的指导和示例,感兴趣的朋友一起看看吧
    2024-10-10
  • Mybatis原始执行方式Executor代码实例

    Mybatis原始执行方式Executor代码实例

    这篇文章主要介绍了Mybatis原始执行方式Executor代码实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • 详解配置类为什么要添加@Configuration注解

    详解配置类为什么要添加@Configuration注解

    这篇文章主要介绍了详解配置类为什么要添加@Configuration注解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • MyBatis 在使用上的注意事项及其辨析(最新最全整理)

    MyBatis 在使用上的注意事项及其辨析(最新最全整理)

    这篇文章主要介绍了MyBatis的在使用上的注意事项及其辨析,本文内容比较长,是小编用心给大家整理的,图文实例代码相结合给大家讲解的非常详细,需要的朋友参考下吧
    2024-06-06
  • 如何将IDEA打成jar包并在windows后台运行

    如何将IDEA打成jar包并在windows后台运行

    在本篇文章里小编给大家分享的是关于如何将IDEA打成jar包并在windows后台运行知识点,需要的朋友们可以学习参考下。
    2019-08-08
  • 详解Spring boot+CXF开发WebService Demo

    详解Spring boot+CXF开发WebService Demo

    这篇文章主要介绍了详解Spring boot+CXF开发WebService Demo,非常具有实用价值,需要的朋友可以参考下
    2017-05-05

最新评论