Spring的投射情势有二种,分别是BeanNameUrlHandlerMapping,
SimpleUrlHandlerMapping和CommonsPathMapHandlerMapping。

1.计算机映射器

<bean
class=”org.springframework.web.servlet.handler.SimpleUrlHandlerMapping”>
    <property name=”urlMap”>
        <map>
            <entry key=”/hello.do”
value=”abstractController”></entry>
        </map>
    </property>
</bean>

 

新手springMVC+freemarker中Could not resolve view with name… 难题化解

博客分类:

  • spring

spring
mvcfreemarker 

新企业用到springMVC+freemarker,自己在做demo过程中报:

 

严重: Servlet.service() for servlet springmvc threw exception

javax.servlet.ServletException: Could not resolve view with name
‘userlist’ in servlet with name ‘springmvc’

澳门新葡萄京官网注册,at
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1102)

at
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:890)

at
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:792)

at
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:851)

at
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:756)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)

at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)

at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)

at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)

at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)

at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)

at
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)

at 

安顿文件如下:

 

<bean id=”userController”
class=”com.demo.controller.UserController”>

<property name=”userService” ref=”userService”></property>

<property name=”myurl” value=”userlist”></property>

</bean>

 

<bean id=”viewResolver”

class=”org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver”>

<property name=”exposeRequestAttributes” value=”true” />

<property name=”exposeSessionAttributes” value=”true” />

<property name=”viewClass”>

<value>org.springframework.web.servlet.view.freemarker.FreeMarkerView</value>

</property>

<property name=”cache”>

<value>true</value>

</property>

<property name=”prefix”>

 <value>/WEB-INF/ftl/

 </value> 

 </property>

<property name=”suffix”>

<value>.html</value>

</property>

<property name=”contentType”>

<value>text/html; charset=UTF-8</value>

</property>

</bean>

 

<bean id=”simpleUrlMapping”

class=”org.springframework.web.servlet.handler.SimpleUrlHandlerMapping”>

<property name=”mappings”>

<props>

<prop key=”/user.htm”>userController</prop>

</props>

</property>

</bean>

 

 

<bean id=”freemarkerConfig”

class=”org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer”>

<property name=”templateLoaderPath”>

<value>/WEB-INF/ftl/</value>

</property>

<property name=”freemarkerSettings”>

<props>

<prop key=”locale”>zh_CN</prop>

<prop key=”defaultEncoding”>UTF-8</prop>

<prop key=”date_format”>yyyy-MM-dd</prop>

<prop key=”time_format”>HH:mm:ss</prop>

<prop key=”datetime_format”>yyyy-MM-dd HH:mm:ss</prop>

<prop
key=”number_format”>0.################</prop>

</props>

</property>

</bean>

 

难点原因是因为freemarker自个儿配置了templateLoaderPath而在viewResolver中没有必要配置prefix,且路线前缀必得配备在templateLoaderPath中

       
第大器晚成种BeanNameUrlHandlerMapping,正是自己在上三个例子中用到的。它是Spring默许的光彩夺目格局。通过三个切实可行的url将调整器联系在一起。轻易的使用推荐应用这种映射格局。不过貌似都毫不它,因为后生可畏旦要映射的乞求超级多,那样就不太有利了。

2.总括深入分析器

<bean
class=”org.springframework.web.servlet.view.InternalResourceViewResolver”>
    <property name=”prefix” value=”/WEB-INF/”></property>
    <property name=”suffix” value=”.jsp”></property>
</bean>

 

       
第二种SimpleUrlHandlerMapping,那些理应说是Spring中最常用的炫酷方式了。看下边包车型客车一个事例:

3.beanName试图拆解深入分析器

<bean
class=”org.springframework.web.servlet.view.BeanNameViewResolver”/>

 

<bean id=”simpleUrlMapping” class=
“org.springframework.web.servlet.handler.SimpleUrlHandlerMapping”>
<property name=”mappings”> <props> <prop
key=”/listCourses.htm”>listCoursesController</prop> <prop
key=”/register.htm”>registerStudentController</prop> <prop
key=”/displayCourse.htm”>displayCourseController</prop>
<prop key=”/login.htm”>loginController</prop> <prop
key=”/enroll.htm”>enrollController</prop> </props>
</property> </bean>

4.参数方法名解析器

<bean id=”propp”
class=”org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver”>
   <!– <property name=”paramName”
value=”actionName”></property>–>
</bean>

 

        
这么些映射格局将索要管理的url列出来了。比如,假使url是listCourses.htm,那么listCoursesController调控器就能够来管理这一个诉求。就那样推算。

5.天性方法名拆解深入分析器

<bean id=”prop”
class=”org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver”>
    <property name=”mappings”>
        <props>
            <prop key=”/one.do”>insert</prop>
            <prop key=”/two.do”>delete</prop>
        </props>
    </property>
</bean>

 

       
至于第三种璀璨形式CommonsPathMapHandlerMapping,书上介绍说是在调整器里面编写对应的url,这种映射形式这段日子并未有用到过,就十分少说了。

6.当中计划深入分析器

<bean id=”iner”
class=”org.springframework.web.servlet.view.JstlView”>
    <property name=”url”
value=”/WEB-INF/1.jsp”></property>
</bean>

 

7.外界试图拆解解析器
<bean id=”jd”
class=”org.springframework.web.servlet.view.RedirectView”>
    <property name=”url”
value=”;
</bean>

 

       
若是想同临时间接选举择黄金时代种以上的映照格局该如何做?Spring的享有映射类都持续了Order接口。我们得以经过设置<property
name=”order”><value>来支配映射格局的优先权。举例:

8.Get和POST请求

<bean id=”abstractController”
class=”cn.bdqn.contrpller.MyAbstractController”>
    <property name=”supportedMethods”>
        <set>
            <value>POST</value>
            <value>GET</value>
        </set>
    </property>
</bean>

<bean id=”beanNameUrlMapping” class=”org.springframework.web.
servlet.handler.BeanNameUrlHandlerMapping”> <property
name=”order”><value>1</value></property>
</bean> <bean id=”simpleUrlMapping”
class=”org.springframework.web.
servlet.handler.SimpleUrlHandlerMapping”> <property
name=”order”><value>0</value></property>
<property name=”mappings”> … </property> </bean>
       
注意,order的值越低,优先权越高。因而,在此间SimpleUrlHandlerMapping的优先权要高于BeanNameUrlHandlerMapping的优先权,所以,先会在SimpleUrlHandlerMapping中寻觅调整器,若无相符的结果再在BeanNameUrlHandlerMapping中搜寻。