您的位置:首页 > 编程语言 > Java开发

基于annotation注解的Spring3 MVC Static Pages Example之四静态页面向学习笔记

2013-12-30 11:04 706 查看
基于annotation注解的Spring3 MVC Static Pages Example之四静态页面向学习笔记

项目结构:



/Spring3MVCDemoStaticPage/src/com/jiangge/common/controller/WebController.java

package com.jiangge.common.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class WebController {

@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index() {
return "index"; //返回 index.jsp视图
}

@RequestMapping(value = "/staticPage", method = RequestMethod.GET)
public String redirect() {
return "redirect:/pages/final.html";
}
}

index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring 静态页面</title>
</head>

<body>
<h2>Spring Landing Pag</h2>
<p>Click below button to get a simple HTML page</p>
<p>点击下面的按钮,得到一个 HTML 页面</p>
<form:form method="GET" action="/staticPage">
<table>
<tr>
<td><input type="submit" value="Get HTML Page" /></td>
</tr>
</table>
</form:form>
</body>

</html>

final.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring MVC </title>
</head>
<body>
<h2>A simple HTML page</h2>
<h2>一个简单的HTML页面,final.html</h2>
</body>
</html>

HelloWeb-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
<context:component-scan base-package="com.jiangge.common.controller" />

<!--  对模型视图名称的解析,即在模型视图名称添加前缀后缀 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

<!-- Here mvc:resources.... tag is being used to map static pages. -->
<!-- The mapping attribute must be an Ant pattern that specifies the URL pattern of an http requests.   -->
<!-- The location attribute must specify one or more valid resource directory locations having static pages including  -->
<!-- images, stylesheets, JavaScript, and other static content. Multiple resource locations may be specified using a comma-seperated list of values. -->
<mvc:resources mapping="/pages/**" location="/WEB-INF/pages/" />
<mvc:annotation-driven/>
</beans>


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring Page Redirection 重定向</display-name>

<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

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

<!-- 编码过滤器,强制编码格式为UTF-8 -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>


运行结果:

http://localhost:8080/Spring3MVCDemo/index





参考链接:

http://www.tutorialspoint.com/spring/spring_static_pages_example.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息