在搭建项目过程中顺利完成,(如何搭建详见《看透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";}
新闻热点
疑难解答