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>
运行结果:
Web.xml文件...
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
...
action-servlet.xml文件
...
<bean id="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="requestContextAttribute"value="rc"/>
<property name="viewClass"value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix"value="/WEB-INF/pages/"/>
<property name="suffix"value=".jsp"/>
</bean>
...
用springMVC 返回一个指定的HTML页面的方法:
1、web.xml的配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>circcenter</display-name>
<welcome-file-list>
<welcome-file>/admin/login.html</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<!-- springMVC DispatcherServlet配置 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
</web-app>
2、springMVC-servlet文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<!--<task:executor id="executor" pool-size="5" />
<task:scheduler id="scheduler" pool-size="10" />
<task:annotation-driven executor="executor" scheduler="scheduler" />
-->
<context:component-scan base-package="com.qingshuang.circ"/>
<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀
<bean 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"/>
</bean>
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="10"></property>
<property name="prefix" value="/"></property>
<property name="contentType" value="text/html"></property>
</bean>
</beans>
3、controller控制器代码:
@Controller
@RequestMapping(value="/test")
public class TestController {
/**
* 测试请求是否成功
*/
@RequestMapping(value="{testAdmin}", method=RequestMethod.GET)
public String testRequest(){
System.out.println("----------------请求成功----------------")
return "admin/login.<span style="font-family:Helvetica, Tahoma, Arial, sans-serif">html</span>"
}
4、html目录:
5、运行结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)