在搭建項(xiàng)目過程中順利完成,(如何搭建詳見《看透sPRingMvc源代碼分析與實(shí)現(xiàn)》這本書中第八章).啟動(dòng)之后遇到的問題如下:
項(xiàng)目可以成功啟動(dòng),但是訪問URL時(shí)后臺(tái)報(bào)錯(cuò)
Configuration problem: Cannot locate BeanDefinitionParser for element [view-resolvers]
出現(xiàn)這個(gè)問題的原因是項(xiàng)目lib里的spring-webmvc-4.2.5.RELEASE.之前用的是spring-webmvc-3.2.0.RELEASE這個(gè)版本,由于版本低問題導(dǎo)致xml中schema找不到[view-resolvers],大家有時(shí)間可以去看看,3.2.0.RELEASE這個(gè)版本的所有xsd均沒有[view-resolvers],4.2.5.RELEASE這個(gè)版本spring-mvc-4.2.xsd和spring-mvc-4.1.xsd這兩個(gè)有
其中的[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用的另一種:
<!-- 對(duì)轉(zhuǎn)向頁面的路徑解析。prefix:前綴, suffix:后綴 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/views/" p:suffix=".jsp" />
用這個(gè)替換標(biāo)紅的,同樣可以實(shí)現(xiàn)。
這個(gè)有一個(gè)問題是在controler中,showArticle方法的articleId一直取不到值。不知道是不是配置中沒用<mvc:view-resolvers> 導(dǎo)致的,覺得原書中能取到也挺不可思議的
@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+"號(hào)文章標(biāo)題");model.addAttribute("article",articleId+"號(hào)文章內(nèi)容");sessionStatus.setComplete();return "article";}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注