Blankman
Blankman

Reputation: 267250

spring petcare sample,how are the controller actions linked to the jsp's?

Looking at springs sample application petcare.

The patient controller looks like:

package org.springframework.samples.petcare.clients.patients;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "/owners/{ownerId}/patients/{patient}")
public class PatientController {

    private final PatientRepository repository;

    @Autowired
    public PatientController(PatientRepository repository) {
        this.repository = repository;
    }

    @RequestMapping(method = RequestMethod.GET)
    public Patient get(Long ownerId, String patient) {
        return repository.getPatient(ownerId, patient);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public Patient getForEditing(Long ownerId, String patient) {
        return repository.getPatient(ownerId, patient);
    }

    @RequestMapping(method = RequestMethod.PUT)
    public void update(Patient patient) {
        repository.savePatient(patient);
    }

    @RequestMapping(method = RequestMethod.DELETE)
    public void delete(Long ownerId, String patient) {
    }

}

How exactly are the actions linked to the jsp's?

Upvotes: 1

Views: 653

Answers (2)

skaffman
skaffman

Reputation: 403591

Without seeing the context definition, it's impossible to say for sure.

However, given that this looks like a REST controller, it's likely that Spring will be marshalling the return values directly to their representation (XML or JSON, using MarshallingView). In this situation, there are no views, in the usual sense.

Alternatively, and again depending how the context is configured, if the controller does not indicate which view to use, then Spring will guess, using the original request URI (e.g. a request to /x will be forwarded to the view /x.jsp). This is part of Spring's "convention over configuration" practice.

To decide which is which, you need to see which ViewResolver implementation is in the context.

Upvotes: 1

Jeff
Jeff

Reputation: 21892

It uses a RequestToViewNameTranslator bean to resolve the appropriate view name. You can optionally define this type of bean in your configuration. If you do not explicitly define a view translator bean, then DispatcherServlet will automatically instantiate a DefaultRequestToViewNameTranslator. DefaultRequestToViewNameTranslator figures out the view name from the request URL.

The Spring reference guide should have some information on this in the Web MVC section.

It's basically another example of Spring's principle of "convention over configuration".

Upvotes: 1

Related Questions