我正在阅读《 Spring框架入门》(第二版)这本书。在ch11-bankapp中,具有@ModelAttribute的函数不会返回视图名称。因此,视图名称应该由RequestToViewNameTranslator返回。
@Controller
@RequestMapping(value = "/fixedDeposit")
public class FixedDepositController {
private static Logger logger = Logger
.getLogger(FixedDepositController.class);
@Autowired
private FixedDepositService fixedDepositService;
@RequestMapping(value = "/list", method = RequestMethod.GET)
@ModelAttribute(value = "fdList")
public List<FixedDepositDetails> listFixedDeposits() {
logger.info("listFixedDeposits() method: Getting list of fixed deposits");
return fixedDepositService.getFixedDeposits();
}
但是,当我输入网址http://localhost:8080/ch11-bankapp/fixedDeposit/list时,它会显示HTTP状态404-错误。
控制台给我以下警告
WARN org.springframework.web.servlet.PageNotFound-在DispatcherServlet中,名称为“ bankapp”的URI [/ ch11-session-attributes / fixedDeposit / list]找不到HTTP请求的映射
有人可以解释一下为什么吗?
ps我正在运行本书的源代码。本书假定代码是完美的。但是,它不起作用。
最佳答案
我尝试了这个例子,它的工作就像一个魅力。
确保您已完成以下操作
1)配置viewNameTranslator
<bean id="viewNameTranslator" class="org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator"/>
如果未提供视图名称,则RequestToViewNameTranslator接口>将自动确定视图名称。为此,您需要在Spring MVC配置文件中配置> DefaultRequestToViewNameTranslator类
摘自上述教程
2)在WEB-INF / jsp文件夹下创建一个文件夹fixedDeposit(如果您配置了其他View解析器,而不是将fixedDeposit文件夹放置在该文件夹中,例如,在我的情况下,我使用了freemarker(WEB-INF / templates),我希望它应该以相同的方式适用于jsp)
RequestToViewNameTranslator这是一个特殊的Bean,可以从请求中解析视图名称。默认情况下,视图名称是通过从请求中删除URI路径并删除媒体类型来解析的。因此,例如,如果对http://host:port/context/servlet/some/path/in/the/app.html发出请求,则默认情况下,视图将解析为“ app”-删除路径和.html后缀。因此您的视图应为http://host:port/context/servlet/some/path/in/the/app
3)您的视图文件名称应为list.jsp(在我的情况下为list.ftl),并将其放置在新的WEB-INF \ jsp \ fixedDeposit文件夹中
现在应该可以使用了。
接下来,在提出您的问题时,也请提供完整的背景,例如我真的很想看看您的application-context.xml文件,以帮助回答您的问题。
如果您有根本原因异常,那么更详细的堆栈跟踪将更受赞赏