如何用springMVC 返回一个指定的HTML页面
2016-05-19 · 百度知道合伙人官方认证企业
用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、运行结果:
2016-04-25 · 做真实的自己 用良心做教育
@RequestMapping(value="/html", method=RequestMethod.GET)
public String prepare(Model model) {
model.addAttribute("foo", "bar");
model.addAttribute("fruit", "apple");
return "views/html"; //view name
}
return new ModelAndView("页面路径");
为什么要返回HTML啊?配置文件里面也得改哪个前后缀