Spring MVC Routing: Trying to pull an element out of URI and into Bean
I have a url like http://localhost:8080/forum/view/1/ (the last integer being an ID)
I want to then have on this page a "Reply" button, and have it be taken to
http://localhost:8080/forum/view/1/Reply
I want to then pull out the ID (in this case '1'), and pass it into the Controller (as variable postID) for the Reply. What I have so far that I've been toying with is this:
<bean name="/view/*/Reply" class="forum.web.NewPostController">
<property name="postId" value="{1}" />
<property name="successView" value=".开发者_StackOverflow中文版./hello.htm" />
<property name="formView" value="addReply" />
<property name="postType" value="R" />
</bean>
Thanks for any help
The easiest way is to use @RequestMapping
annotation. Here is the example from Spring docs:
@Controller
@RequestMapping("/appointments")
public class AppointmentsController {
private final AppointmentBook appointmentBook;
@Autowired
public AppointmentsController(AppointmentBook appointmentBook) {
this.appointmentBook = appointmentBook;
}
@RequestMapping(method = RequestMethod.GET)
public Map<String, Appointment> get() {
return appointmentBook.getAppointmentsForToday();
}
@RequestMapping(value="/{day}", method = RequestMethod.GET)
public Map<String, Appointment> getForDay(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date day, Model model) {
return appointmentBook.getAppointmentsForDay(day);
}
@RequestMapping(value="/new", method = RequestMethod.GET)
public AppointmentForm getNewForm() {
return new AppointmentForm();
}
@RequestMapping(method = RequestMethod.POST)
public String add(@Valid AppointmentForm appointment, BindingResult result) {
if (result.hasErrors()) {
return "appointments/new";
}
appointmentBook.addAppointment(appointment);
return "redirect:/appointments";
}
}
More info can be found at http://static.springsource.org/spring/docs/3.0.x/reference/mvc.html
精彩评论