看透springMvc源代码分析与实现这本书中第八章实例总结(controller和View)

在搭建项目过程中顺利完成,(如何搭建详见《看透springMvc源代码分析与实现》这本书中第八章).启动之后遇到的问题如下:

项目可以成功启动,但是访问URL时后台报错

Configuration problem: Cannot locate BeanDefinitionParser for element [view-resolvers]

出现这个问题的原因是项目lib里的spring-webmvc-4.2.5.RELEASE.之前用的是spring-webmvc-3.2.0.RELEASE这个版本,由于版本低问题导致xml中schema找不到[view-resolvers],大家有时间可以去看看,3.2.0.RELEASE这个版本的所有xsd均没有[view-resolvers],4.2.5.RELEASE这个版本spring-mvc-4.2.xsd和spring-mvc-4.1.xsd这两个有

其中的[view-resolvers]在配置文件中配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"               
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" 
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/util
  http://www.springframework.org/schema/util/spring-util-4.2.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-4.2.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd ">
   <mvc:annotation-driven></mvc:annotation-driven>
  <context:component-scan base-package="com.game"></context:component-scan> 
  <!-- <mvc:view-resolvers>
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="WEB-INF/views/jsp" p:suffix=".jsp"></bean>
  </mvc:view-resolvers> -->
 <mvc:view-resolvers>
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/views/" p:suffix=".jsp"></bean>
  </mvc:view-resolvers>

  <!--  <bean id = "helloworld" class="com.game.controller.Helloworld">
  </bean>  -->
</beans>


---------------------------------------------------------------------------------------------------------------

由于 <mvc:view-resolvers>需要较高的jar版本,配置起来需要好多新的jar,所以我配置viewresolver用的另一种:

<!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/views/" p:suffix=".jsp" />

 

用这个替换标红的,同样可以实现。

这个有一个问题是在controler中,showArticle方法的articleId一直取不到值。不知道是不是配置中没用<mvc:view-resolvers>   导致的,觉得原书中能取到也挺不可思议的

@RequestMapping(value={"/articles/{articleId}/comment"})
public String doComment(@PathVariable String articleId,RedirectAttributes attributes,Model model)throws Exception{
String comment=(String) model.asMap().get("comment");
comment=new String (comment.getBytes("UTF-8"));
attributes.addFlashAttribute("comment", (String) model.asMap().get("comment"));
model.addAttribute("articleId", articleId);
return "redirect:/showArticle";
}

@RequestMapping(value={"/showArticle"},method={RequestMethod.GET})
public String showArticle(Model model,SessionStatus sessionStatus) throws Exception{
String articleId =(String)model.asMap().get("articleId");
model.addAttribute("articleTitle", articleId+"号文章标题");
model.addAttribute("article",articleId+"号文章内容");
sessionStatus.setComplete();
return "article";
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值