spring mvc 如何通过ACTION访问在WEB-INF下的html

spring mvc 如何通过ACTION访问在WEB-INF下的html,第1张

通过试图解析器 以下是代码请供参考!

@RequestMapping("h_handelList")

    public ModelAndView h_handelList () throws Exception {

        ModelAndView mv = new ModelAndView()

        mv.setViewName("/WEB-INF/jsp/h_handelList.html")

        return mv

    }

也可以在springMVC.xml里配置试图解析器的前缀和后缀

<!-- 配置springmvc的视图解析器 -->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix" value="/WEB-INF/jsp/"/>

        <property name="suffix" value=".html"/>

    </bean>

配置了前缀后缀后可以这样写:

@RequestMapping("h_handelList")

    public ModelAndView h_handelList () throws Exception {

        ModelAndView mv = new ModelAndView()

        mv.setViewName("h_handelList")

        return mv

    }

试图解析器是必须要配置的 可以不配置前缀后缀!

spring4零配置配置默认html页面的方法是在视图解析器中配置实现的。

比如,项目中以打开login.html为例来说明:

以下是spring-servlet.xml的配置,其中重点是property中的html属性配置。

<?xml version="1.0" encoding="UTF-8"?>

<!--<context:annotation-config />-->

<!-- 把标记了@Controller注解的类转换为bean -->

<context:component-scan base-package="controller" />

<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

<!-- 设置freeMarker的配置文件路径 -->

<bean id="freemarkerConfiguration"

class="org.springframework.beans.factory.config.PropertiesFactoryBean">

<!--注释掉的下方代码是指引freemarker的基本信息的配置位置,因为我已经将配置信息移到了applicationContext文件下,所以这里就没必要存在了,不注释也不会有问题的 -->

<!--<property name="location" value="classpath:/WEB-INF/config/freemarker.properties" />-->

</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>

<!--这里需要注意一下,我注释了下面这样一行代码,这行代码的意思就是指引freemarker需要解析的文件的位置。注释掉原因是因为

applicationContext.xml里有这样一行代码:<property name="templateLoaderPath" value="/WEB-INF/views/" />已经指定了视图位置。如果我们这里依然保留下方代码,页面回报406的找不到的错误 -->

<!--<property name="prefix"><value>/WEB-INF/views/</value></property>-->

<property name="suffix"><value>.html</value></property>

<property name="contentType">

<value>text/htmlcharset=UTF-8</value>

</property>

</bean>

</beans>

运行结果:


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/6095294.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-14
下一篇 2023-03-14

发表评论

登录后才能评论

评论列表(0条)

保存