Webアプリ(Eclipseで開発中)で、ユーザーがブラウザーでURLを利用できるようにします。WebアプリはJavaSpring MVCに基づいており、コントローラーはhtmlページを返します。すべてのhtmlページはWebContent / WEB-INF / viewsフォルダーにあります。すべてのcss \ javacript \ imagesはWebContent / resources / {css \ javacript \ images}フォルダーにあります。
このWebアプリがアクセスする必要のあるURLは次のとおりです
今、私はベンダーのカテゴリフィルターを実装したいと思います
さらにvendor.html({all、med、army、other}ベンダーの場合もあります)で名前のリンクをクリックし、URLを次のようにします。
localhost:8080 / Project / vendor / med / vendor_XXは、選択したvendor_XXの完全な情報を表示します-(vendor_XX.htmlでコード化)
すべての送信はGETタイプです
home / about / vendor_XX.html
<html>
<link rel="stylesheet" href="resources/css/mystyle.css" type="text/css" />
<a href="home">Home</a>
<a href="vendor">Vendor</a>
<a href="about">About</a>
<a href="vendor/med">Medical</a>
<a href="vendor/army">Army</a>
<a href="vendor/other">Other</a>
// and other non relevant stuff
</html>
vendor.html
<html>
<link rel="stylesheet" href="resources/css/mystyle.css" type="text/css" />
<a href="home">Home</a>
<a href="vendor">Vendor</a>
<a href="about">About</a>
<a href="vendor/med">Medical</a>
<a href="vendor/army">Army</a>
<a href="vendor/other">Other</a>
// generating below 3 line dynamically with js
<a href="vendor/med/vendor_xx">Vendor_XX</a>
<a href="vendor/med/vendor_yy">Vendor_YY</a>
<a href="vendor/other/vendor_zz">Vendor_ZZ</a>
// and other non relevant stuff
</html>
私のコントローラー
@Controller
public class AppController {
@RequestMapping(value = "home", method = RequestMethod.GET)
public String home() {
return "home";
}
@RequestMapping(value = "vendor", method = RequestMethod.GET)
public String vendor() {
return "vendor";
}
@RequestMapping(value = "vendor/med", method = RequestMethod.GET)
public String vendorMed() {
return "vendor";
}
@RequestMapping(value = "vendor/army", method = RequestMethod.GET)
public String vendorArmy() {
return "vendor";
}
@RequestMapping(value = "vendor/med/vendor_xx", method = RequestMethod.GET)
public String vendorMedXX() {
return "vendor_xx";
}
//all sample urls are given
}
プロジェクトのビルドパスにResourcesフォルダーが追加されました
localhost:8080 / Project / vendor / med / vendor_XX上記のURLをlocalhost:8080 / Project / level_1 / level_2 / level_3と見なします
問題1)-level_1を除くすべてのURLでcssが見つかりません。level_2 url need css import as <link rel="stylesheet" href="../resources/css/mystyle.css" type="text/css" />
level_3 url need css import as<link rel="stylesheet" href="../../resources/css/mystyle.css" type="text/css" />
質問1-リソースからcssをスプリングロードしないのはなぜですか。私は何かが足りませんか?
2)-クリックした場合
<a href="home">Home</a>
level_1 / level_2 vendor.htmlから、level_1 / homeに送信されます。したがって、コントローラー要求のマッピングには含まれていません。
質問2 -localhost:8080 / Project / homeにリダイレクトするにはどうすればよいですか?
URLは、コマンドラインのパスとほとんど同じように機能します。
ディレクトリにい/foo/bar/
てコマンドを実行すると、相対パスを使用しているためless file.txt
、ファイルが開き/foo/bar/file.txt
ます。
ファイルを開きたい場合は/file.txt
、less ../../file.txt
2つのディレクトリを上に移動するか、単にless /file.txt
ルートから開始する必要があります。
URL(つまり、ブラウザのロケーションバーに表示されるもの)がhttp://localhost/Project/vendor/med/vendor_xx
であるページを表示していて、でファイルresources/css/mystyle.css
をロードhttp://localhost/Project/vendor/med/resources/css/mystyle.css
すると、相対ファイルを使用しているため、ブラウザはからファイルをロードします。ディレクトリ」はhttp://localhost/Project/vendor/med/
です。
正しいURLからロードするには、つまりhttp://localhost/Project/resources/css/mystyle.css
、を使用../../resources/css/mystyle.css
して2つのディレクトリを上に移動するか、ルートから開始する絶対パスが必要です/Project/resources/css/mystyle.css
。
プロジェクトのコンテキストパス(つまり/Project
)のハードコーディングを回避するには、JSTLのc:urlタグを使用するか、単に
href="${pageContext.request.contextPath}/resources/css/mystyle.css"
私は、ほとんどの場合、上記のような絶対パスを使用することをお勧めします。これは、「ディレクトリ」にいるときはいつでも機能します。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加