0

In the jsp, once you click on a category, it gets appended dynamically to the end of the bookRecommendation path, so you get /bookRecommendation/fiction or /bookRecommendation/biography, etc.

PROBLEM: after doing so, when I click another link, it just gets appended to the path, so bookRecommendation/aboutMe, when I just want it to take me to /aboutMe. Everything was working as expected, before I added the method containing the path variable to the 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>

header.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, if that's helpful:

<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>

Any ideas? Thanks!

1
  • can you add the jsp views Commented Mar 12, 2017 at 2:57

3 Answers 3

1

there is nothing wrong with the controller. you're using is a relative path.you need to use an absolute path on your server:

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

change this to this

<a href="/bookRecommendations/${category.name}">${category.name}</a>
Sign up to request clarification or add additional context in comments.

Comments

1

It happens because the last path in your code is /bookRecommendations/{category}. So when you again press the /aboutme , the url gets appended by removing the last url i.e. in your case /{category} and adds "/bookRecommendations/aboutme. So your solution will be just add pagecontext in the link like:

<a href="${pageContext.request.contextPath}/aboutme">Go to about me </a>

Happy learning!!!

Comments

1

You can try this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.