Springbootはjspを統合して、バス停のルートマップを実現します

この記事では主に、jspを統合し、バス停のルートマップを実現して、誰もがSpringbootフレームワークをよりよく理解して使用できるようにするための、springbootの手順を紹介します。

開発環境:

  1. jdk 8
  2. インテリジアイデア
  3. Tomcat 8
  4. mysql 5.7
  5. Maven 3.6

使用したテクノロジー:

  • スプリングブーツ

  • jsp

  • データの静的初期化

プロジェクト紹介

springbootを使用してjspを統合し、バスルート名と詳細サイトをバックエンドに書き込み、フロントエンドページでバスルート、バス名、バス情報などの特定のコンテンツを照会します。

実行結果

フォアグラウンドクライアント:

  • ルートの選択

image.png

  • ルートの詳細

image.png

データの準備:

BusData.txt

image.png

準備オーケー:

Pom.xmlは、jspテンプレートエンジンのサポートを追加します。

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

スプリングブート構成jsp

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

重要なコード:
バスデータの初期化

@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
。その他の後続の反復関数フォローアップ開発、しばらくお待ちください

2021年に収集された最新の高頻度インタビューの質問(すべてドキュメントにまとめられています)には、mysql、netty、spring、thread、spring cloud、jvm、ソースコード、アルゴリズム、その他の詳細な説明など、多くの乾物があります。また、詳細な学習計画とインタビュー。質問など、これらのコンテンツを取得する必要がある友人は、Q Junyang:547998459を追加してください。

おすすめ

転載: blog.csdn.net/p1830095583/article/details/114877533