gpt4 book ai didi

java - 使用@pathvariable后,Spring附加所有链接而不是返回预期目的地

转载 作者:行者123 更新时间:2023-12-01 20:53:46 24 4
gpt4 key购买 nike

在 jsp 中,一旦您单击某个类别,它就会动态附加到 bookRecommendation 路径的末尾,因此您会得到/bookRecommendation/fiction 或/bookRecommendation/biography 等。

问题:这样做后,当我单击另一个链接时,它只是附加到路径中,所以 bookRecommendation/aboutMe,当我只想它带我到/aboutMe 时。在我将包含路径变量的方法添加到 Controller 之前,一切都按预期工作。

@Controller 
public class SiteController {

@RequestMapping(path="/bookRecommendations/{category}", method=RequestMethod.GET)
public String displayCategoryPage(@PathVariable String category, ModelMap model) {
List<Book> bookList = new ArrayList<>();
bookList = bookDAO.getBooksByCategory(category);
model.put("category", category);
model.put("books", bookList);
return "bookCategoryTemplate";
}

@RequestMapping("/aboutMe")
public String displayAboutMe() {
return "aboutMe";
}
}

bookRecommendations.jsp:

    <div class="row">
<div class="col-md-12">
<div class="dropdown">
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Go To...
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<c:forEach items="${categories}" var="category">
<li><a href="bookRecommendations/${category.name}">${category.name}</a></li>
</c:forEach>
</ul>
</div>
</div>
</div>

bookCategoryTemplate.jsp:

<div class="book-category-template">
<h1 class="book-category-header">${category}</h1>
<c:forEach items="${books}" var="book">
<h3>${book.title}</h3>
</c:forEach>
</div>

标题.jsp

<header class="navbar navbar-default">
<div class="container">
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li><a href="bookRecommendations">Book Recommendations</a></li>
<li><a href="aboutMe">About Me</a></li>
<li><a href="login">Login</a>
</ul>
</div>
</div>
</header>

viewResolver.xml,如果有帮助的话:

<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

有什么想法吗?谢谢!

最佳答案

Controller 没有任何问题。您使用的是相对路径。您需要在服务器上使用绝对路径:

<a href="bookRecommendations/${category.name}">${category.name}</a>

改成这个

<a href="/bookRecommendations/${category.name}">${category.name}</a>

关于java - 使用@pathvariable后,Spring附加所有链接而不是返回预期目的地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42743097/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com