2016-10-10 62 views
3

我使用Servlets和Tomcat 7作爲服務器。 My hierarchy is使用Tomcat 7找不到樣式表文件

因此,當我訪問'/resources/js/bootstrap.min.js'時,WebApp無法找到這些文件。

這是我在WEB-INF文件夾中的index.jsp。

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 
 
<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
<meta charset="UTF-8"> 
 
<title>WebStore</title> 
 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
 
</head> 
 
<body> 
 
\t <div class="container-fluid"> 
 
\t \t <nav class="navbar navbar-default"> 
 
\t \t \t <div class="container-fluid"> 
 
\t \t \t \t <!-- Brand and toggle get grouped for better mobile display --> 
 
\t \t \t \t <div class="navbar-header"> 
 
\t \t \t \t \t <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> 
 
\t \t \t \t \t \t <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> 
 
\t \t \t \t \t </button> 
 
\t \t \t \t \t <a class="navbar-brand" href="/">WebStore</a> 
 
\t \t \t \t </div> 
 
\t \t \t \t <!-- Collect the nav links, forms, and other content for toggling --> 
 
\t \t \t \t <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> 
 
\t \t \t \t \t <ul class="nav navbar-nav"> 
 
\t \t \t \t \t \t <li class="active"><a href="#">Home <span class="sr-only">(current)</span></a></li> 
 
\t \t \t \t \t \t <li><a href="#">Help</a></li> 
 
\t \t \t \t \t \t <li><a href="#">Contact us</a></li> 
 
\t \t \t \t \t \t <li><a href="#">About</a></li> 
 
\t \t \t \t \t \t <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Profile 
 
\t \t \t \t \t \t \t \t <span class="caret"></span> 
 
\t \t \t \t \t \t </a> 
 
\t \t \t \t \t \t \t <ul class="dropdown-menu"> 
 
\t \t \t \t \t \t \t \t <li><a href="#">Action</a></li> 
 
\t \t \t \t \t \t \t \t <li><a href="#">Another action</a></li> 
 
\t \t \t \t \t \t \t \t <li><a href="#">Something else here</a></li> 
 
\t \t \t \t \t \t \t \t <li role="separator" class="divider"></li> 
 
\t \t \t \t \t \t \t \t <li><a href="#">Separated link</a></li> 
 
\t \t \t \t \t \t \t \t <li role="separator" class="divider"></li> 
 
\t \t \t \t \t \t \t \t <li><a href="#">One more separated link</a></li> 
 
\t \t \t \t \t \t \t </ul></li> 
 
\t \t \t \t \t </ul> 
 
\t \t \t \t \t <form class="navbar-form navbar-right"> 
 
\t \t \t \t \t </form> 
 
\t \t \t \t </div> 
 
\t \t \t \t <!-- /.navbar-collapse --> 
 
\t \t \t </div> 
 
\t \t \t <!-- /.container-fluid --> 
 
\t \t </nav> 
 
\t \t <div class="row"> 
 
\t \t \t <div class="col-md-2"> 
 
\t \t \t \t <div class="panel panel-default"> 
 
\t \t \t \t \t <div class="panel-heading"> 
 
\t \t \t \t \t \t <h3 class="panel-title">Categories</h3> 
 
\t \t \t \t \t </div> 
 
\t \t \t \t \t <div class="panel-body"> 
 
\t \t \t \t \t \t <ul class="list-group"> 
 
\t \t \t \t \t \t \t <li class="list-group-item"><a href="#">Category1</a></li> 
 
\t \t \t \t \t \t \t <li class="list-group-item"><a href="#">Category2</a></li> 
 
\t \t \t \t \t \t \t <li class="list-group-item"><a href="#">Category3</a></li> 
 
\t \t \t \t \t \t \t <li class="list-group-item"><a href="#">Category4</a></li> 
 
\t \t \t \t \t \t \t <li class="list-group-item"><a href="#">Category5</a></li> 
 
\t \t \t \t \t \t </ul> 
 
\t \t \t \t \t </div> 
 
\t \t \t \t </div> 
 
\t \t \t \t <div class="panel panel-default"> 
 
\t \t \t \t \t <div class="panel-heading"> 
 
\t \t \t \t \t \t <h3 class="panel-title">Brands</h3> 
 
\t \t \t \t \t </div> 
 
\t \t \t \t \t <div class="panel-body"> 
 
\t \t \t \t \t \t <ul class="list-group"> 
 
\t \t \t \t \t \t \t <li class="list-group-item"><a href="#">Brand1</a></li> 
 
\t \t \t \t \t \t \t <li class="list-group-item"><a href="#">Brand2</a></li> 
 
\t \t \t \t \t \t \t <li class="list-group-item"><a href="#">Brand3</a></li> 
 
\t \t \t \t \t \t \t <li class="list-group-item"><a href="#">Brand4</a></li> 
 
\t \t \t \t \t \t \t <li class="list-group-item"><a href="#">Brand5</a></li> 
 
\t \t \t \t \t \t </ul> 
 
\t \t \t \t \t </div> 
 
\t \t \t \t </div> 
 
\t \t \t </div> 
 
\t \t \t <div class="col-md-8"> 
 
\t \t \t \t <div class="col-md-4 col-xs-6"> 
 
\t \t \t \t \t <div class="thumbnail"> 
 
\t \t \t \t \t \t <a href="" class="commodity"> <img class="goodsPhoto" src="images/apple-iphone se 16gb-silver-450x350.png" alt="iPhone"> 
 
\t \t \t \t \t \t </a> 
 
\t \t \t \t \t \t <div class="caption"> 
 
\t \t \t \t \t \t \t <h5>iPhone SE 16Gb</h5> 
 
\t \t \t \t \t \t \t <p class="definition">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Atque dolorum maiores repellat, autem, veniam incidunt quam 
 
\t \t \t \t \t \t \t \t sed fugit eos expedita, optio.</p> 
 
\t \t \t \t \t \t \t <p class="pull-left price">352.15$</p> 
 

 
\t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t <p class="text-right"> 
 
\t \t \t \t \t \t \t <a class="btn btn-primary" href="">Add to bucket</a> 
 
\t \t \t \t \t \t </p> 
 
\t \t \t \t \t </div> 
 
\t \t \t \t </div> 
 
\t \t \t \t <div class="col-md-4 col-xs-6"> 
 
\t \t \t \t \t <div class="thumbnail"> 
 
\t \t \t \t \t \t <a href="" class="commodity"> <img class="goodsPhoto" src="images/apple-iphone se 16gb-silver-450x350.png" alt="iPhone"> 
 
\t \t \t \t \t \t </a> 
 
\t \t \t \t \t \t <div class="caption"> 
 
\t \t \t \t \t \t \t <h5>iPhone SE 16Gb</h5> 
 
\t \t \t \t \t \t \t <p class="definition">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Atque dolorum maiores repellat, autem, veniam incidunt quam 
 
\t \t \t \t \t \t \t \t sed fugit eos expedita, optio.</p> 
 
\t \t \t \t \t \t \t <p class="pull-left price">352.15$</p> 
 

 
\t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t <p class="text-right"> 
 
\t \t \t \t \t \t \t <a class="btn btn-primary" href="">Add to bucket</a> 
 
\t \t \t \t \t \t </p> 
 
\t \t \t \t \t </div> 
 
\t \t \t \t </div> 
 
\t \t \t </div> 
 
\t \t \t <div class="col-md-2"> 
 
\t \t \t \t <div class="panel panel-default"> 
 
\t \t \t \t \t <div class="panel-heading"> 
 
\t \t \t \t \t \t <h3 class="panel-title">Information</h3> 
 
\t \t \t \t \t </div> 
 
\t \t \t \t \t <div class="panel-body"> 
 
\t \t \t \t \t \t <ul class="list-group"> 
 
\t \t \t \t \t \t \t <li class="info">In bucket: 9</li> 
 
\t \t \t \t \t \t \t <li class="info">Money: 15524$</li> 
 
\t \t \t \t \t \t \t <li class="info"><a href="#">My profile</a></li> 
 
\t \t \t \t \t \t \t <li class="info"><a href="#">My bucket</a></li> 
 
\t \t \t \t \t \t \t <li class="info"><a href="#">My wishlist</a></li> 
 

 
\t \t \t \t \t \t </ul> 
 
\t \t \t \t \t </div> 
 
\t \t \t \t </div> 
 
\t \t \t \t <div class="panel panel-default"> 
 
\t \t \t \t \t <div class="panel-heading"> 
 
\t \t \t \t \t \t <h3 class="panel-title">Tags</h3> 
 
\t \t \t \t \t </div> 
 
\t \t \t \t \t <div class="panel-body"> 
 
\t \t \t \t \t \t <ul class="list-group"> 
 
\t \t \t \t \t \t \t <li class="list-group-item"><a href="#">Tag1</a></li> 
 
\t \t \t \t \t \t \t <li class="list-group-item"><a href="#">Tag2</a></li> 
 
\t \t \t \t \t \t \t <li class="list-group-item"><a href="#">Tag3</a></li> 
 
\t \t \t \t \t \t \t <li class="list-group-item"><a href="#">Tag4</a></li> 
 
\t \t \t \t \t \t \t <li class="list-group-item"><a href="#">Tag5</a></li> 
 
\t \t \t \t \t \t </ul> 
 
\t \t \t \t \t </div> 
 
\t \t \t \t </div> 
 
\t \t \t </div> 
 
\t \t </div> 
 
\t </div> 
 
\t <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
 
\t <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
 
\t <!-- Include all compiled plugins (below), or include individual files as needed --> 
 
\t <script src="resources/js/bootstrap.min.js"></script> 
 
</body> 
 
</html>

And here is my error:

我的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 
    <display-name>WebAppTesting</display-name> 
    <welcome-file-list> 
    <welcome-file>/</welcome-file> 
    </welcome-file-list> 
</web-app> 

當我點擊這個錯誤,Chrome瀏覽器重定向我到我的index.jsp文件。怎麼了?

+0

你嘗試尋找到瀏覽器的開發者工具網絡選項卡(在Chrome F12)到當請求'bootstrap.min.js'時,看看服務器返回了什麼? –

+0

我很抱歉)他找到了我的文件。但是這個錯誤意味着什麼?我無法理解......爲什麼我在那裏有語法錯誤? –

+0

是的,我看了那裏,狀態碼是200.當我點擊那個文件,我看到bootstrap的來源,但是當我點擊錯誤時,他只是發送我到我的索引文件 –

回答

0

我解決了我的問題。據我瞭解,當我進入本地主機時,IndexServlet捕獲請求,然後轉發到/WEB-INF/index.jsp。然後,從jsp,我鏈接到位於/ resources/css/.....的css文件。我不知道爲什麼,但是我的IndexServlet捕獲這個請求並將其轉發到index.jsp,這就是爲什麼我收到兩個相同的文件。

另一個問題是:爲什麼servlet發現了這個請求?在servlet上面有一個註釋,例如:@WebServlet(「/」)。沒有「/」*捕獲所有請求。

誰知道,請讓我知道發生了什麼?

也許他認識這個鏈接(/資源/ CSS/...)作爲PATHINFO,這就是爲什麼他抓住了我的所有鏈接