Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
696 views
in Technique[技术] by (71.8m points)

spring mvc直接访问jsp,返回的是jsp的源码?

首先在web.xml中配置了spring的dispatchservlet,匹配的是/*,如下所示:

<servlet-mapping>
        <servlet-name>spring-servlet</servlet-name>
        <url-pattern>/*</url-pattern>
</servlet-mapping>

然后在dispatch-servlet.xml中配置视图解析器,对jsp后缀的文件进行解析,已使用了最高优先级:

<bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
        <property name="order" value="0"/>
    </bean>

最后,直接访问index.jsp,为什么返回的是index.jsp内部的源码,而不是解析后的视图呢?我知道修改<servlet-mapping>的映射地址,如改成<url-pattern>*.action</url-mapping>,这样是可以的,因为jsp链接是不走dispatch-servlet,但为什么我将InternalResourceViewResolver视图解析器的优先级调到最高了,jsp链接还是不会使用此解析器进行解析呢?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

改成

<servlet-mapping>
        <servlet-name>spring-servlet</servlet-name>
        <url-pattern>/</url-pattern>
</servlet-mapping>

/*/的区别在于:
/不会拦截jsp视图,而/*会拦截。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...