JavaSpringPetCare示例,控制器操作如何链接到jsp的?
查看springs示例应用程序petcare
患者控制器看起来像:
包组织。springframework。样品。宠物护理。客户。病人
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) {
}
}
这些操作到底是如何链接到jsp的
# 1 楼答案
如果看不到上下文的定义,就不可能确定
然而,考虑到这看起来像一个REST控制器,Spring很可能会将返回值直接编组到它们的表示(XML或JSON,使用
MarshallingView
)。在这种情况下,通常意义上没有视图或者,同样取决于上下文的配置方式,如果控制器没有指示要使用哪个视图,那么Spring将使用原始请求URI进行猜测(例如,对
/x
的请求将被转发到视图/x.jsp
)。这是Spring“约定优先于配置”实践的一部分要决定哪个是哪个,需要查看上下文中的
ViewResolver
实现# 2 楼答案
它使用
RequestToViewNameTranslator
bean来解析适当的视图名称。您可以选择在配置中定义这种类型的bean。如果没有显式定义视图转换器bean,那么DispatcherServlet
将自动实例化DefaultRequestToViewNameTranslator
DefaultRequestToViewNameTranslator
从请求URL中找出视图名称Spring参考指南应该在WebMVC一节中提供一些关于这方面的信息
这基本上是Spring“约定优先于配置”原则的另一个例子