Reputation: 213281
I was following the application example in the book - Spring Recipes, Section 8-10. And I am stuck in an issue. I'll try to keep it short.
I've Reservation bean containing following fields:
String courtName, Date date, int hour, Player player, SportType sportType
Player bean contains just two fields - name
, and age
.
SportType is an enum:
public enum SportType {
TENNIS("Tennis"),
SOCCER("Soccer");
// Constructor, Getter
}
Now, I've a view - reservationForm.jsp, with corresponding controller - ReservationFormController.
The controller has the following annotations:
@Controller
@RequestMapping("/reservationForm")
@SessionAttributes("reservation")
Also, it has some methods, relevant out of them are:
@RequestMapping(method=RequestMethod.GET)
public String setupForm(
@RequestParam(required = false, value="username") String username,
@RequestParam(required = true, value="age") int age,
Model model) {
System.out.println("Setting Up Form");
Reservation reservation = new Reservation();
reservation.setPlayer(new Player(username, age));
model.addAttribute("reservation", reservation);
return "reservationForm";
}
@RequestMapping(method=RequestMethod.POST)
public String submitForm(Model model,
@ModelAttribute("reservation") Reservation reservation,
BindingResult result, SessionStatus status) {
reservationValidator.validate(reservation, result);
if (result.hasErrors()) {
model.addAttribute("reservation", reservation);
return "reservationForm";
} else {
reservationService.make(reservation);
status.setComplete(); // Expires the session
return "redirect:successfulReservation";
}
}
@RequestMapping("successfulReservation")
public String reservationSuccess() {
System.out.println("Called");
return "reservationSuccess";
}
Now, when I deploy the application, and access the url:
http://localhost:8080/springmvc/reservationForm?age=21
The form is displayed from reservationForm.jsp. And after I hit submit button in that form, submitForm()
method will be called.
The problem is, in that method, if validation succeeds, the return statement in else
block:
return "redirect:successfulReservation"
is not working. It's showing an HTTP Status-404.
The url shown in the browser after I submit the form is:
http://localhost:8080/springmvc/successfulReservation?sportTypes=TENNIS&sportTypes=SOCCER
I don't understand how those 2 request attributes came in the query string.
When I tested whether the method handler- reservationSuccess()
corresponding to successfulReservation is called or not, I saw that it's not being called. Due to which, the view is not being resolved.
Also, in the Eclipse console, it's showing a warning:
WARNING: No mapping found for HTTP request with URI [/springmvc/successfulReservation] in DispatcherServlet with name 'court'
I can't understand what might be the problem.
I've added - <context:component-scan>
in the configuration file.
This is the view resolver I've configured:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
Also, it seems like, examples in that book are not complete. They haven't defined the reservationSuccess()
method for redirected url. That I've added on my own.
Can someone help me out here, what's going wrong?
I've tried to compact as much relevant information as I can here. If you want some more information, do ask. I'll add.
Upvotes: 0
Views: 145
Reputation: 51030
Looks like you should redirect to /reservationForm/successfulReservation
.
Since the method is within the controller, and controller also has request-mapping, may be the method's request-mapping is not independent of that of the controller.
Upvotes: 2