Clay Banks
Clay Banks

Reputation: 4591

Spring @Controller Not Returning View as Expected

I'm trying to return a view under my /WEB-INF/jsp/ folder named person.jsp on click of of an anchor tag. My view resolver handles all views under this folder. Here's my tag:

<a class="add" href="#">Click</a>

My AJAX GET Request:

$(document).on("click", ".add", function(e) {
     request = /person/ + personID
     //success, failure, etc.
}

And here's my @Controller

@Controller 
public class RCITTFormController {

    @RequestMapping(value = "/person/{id}", method = RequestMethod.GET)
    public String getPerson(@PathVariable String id){
        return "person";    
    }
}

However my persons.jsp is only returned in the HTTP Response (it's behaving as if I'd set my class as a @RestController). I've tried the "redirect:/person", returned a ModelAndView instance with person passed as the view, but to no avail. Is my problem something simpler, like the anchor tag itself? Could include more details if needed.

Thanks much

Upvotes: 0

Views: 942

Answers (2)

Ivan Rodrigues
Ivan Rodrigues

Reputation: 469

Try to return a ModelAndView like return new ModelAndView("person"). This if your JSP really is named as person.

Upvotes: 0

Aeseir
Aeseir

Reputation: 8434

solution to this is as follows:

$(document).on("click", ".add", function(e) {
     request = /person/ + personID
     window.location.href = request;
}

That will direct you to the page you are after.

window.location.href will force your site to go to page you are after, but then you don't need ajax for this. Just use your standard anchor href.

Upvotes: 1

Related Questions