Spring MVC Tiles示例

spring mvc tiles示例

 

spring提供了与apache tile框架的集成支持。因此,我们可以借助spring tile支持简单地管理spring mvc应用程序的布局。

 

spring mvc支持tiles的优势

可重用性: : 我们可以在多个页面中重复使用单个组件,例如页眉和页脚组件。

集中式控件: 我们可以通过单个模板页面来控制页面的布局

易于更改布局: : 借助单个模板页面,我们可以随时更改页面的布局。因此,您的网站可以轻松采用新技术,例如引导程序,jquery等。

 

spring mvc tiles示例

 

1、将依赖项添加到pom.xml文件。

pom.xml

 <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupid>org.springframework</groupid>
    <artifactid>spring-webmvc</artifactid>
    <version>5.1.1.release</version>
</dependency>
  <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>  
    <groupid>javax.servlet</groupid>  
    <artifactid>servlet-api</artifactid>  
    <version>3.0-alpha-1</version>  
</dependency>
    <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
    <groupid>javax.servlet</groupid>
    <artifactid>jstl</artifactid>
    <version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
    <groupid>org.apache.tomcat</groupid>
    <artifactid>tomcat-jasper</artifactid>
    <version>9.0.12</version>
</dependency>
 <!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-jsp -->
<dependency>
    <groupid>org.apache.tiles</groupid>
    <artifactid>tiles-jsp</artifactid>
    <version>3.0.5</version>
</dependency>
    
    <!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-servlet -->
<dependency>
    <groupid>org.apache.tiles</groupid>
    <artifactid>tiles-servlet</artifactid>
    <version>3.0.5</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-core -->
<dependency>
    <groupid>org.apache.tiles</groupid>
    <artifactid>tiles-core</artifactid>
    <version>3.0.5</version>
</dependency>
   <!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-el -->
<dependency>
    <groupid>org.apache.tiles</groupid>
    <artifactid>tiles-el</artifactid>
    <version>3.0.5</version>
</dependency>

 

2、创建bean类

contact.java

package com.yapf.form;
public class contact {
    private string firstname;
    private string lastname;
    private string email;
    private string telephone;
    
    public string getemail() {
        return email;
    }
    public string gettelephone() {
        return telephone;
    }
    public void setemail(string email) {
        this.email = email;
    }
    public void settelephone(string telephone) {
        this.telephone = telephone;
    }
    public string getfirstname() {
        return firstname;
    }
    public string getlastname() {
        return lastname;
    }
    public void setfirstname(string firstname) {
        this.firstname = firstname;
    }
    public void setlastname(string lastname) {
        this.lastname = lastname;
    }
    
}

 

3、创建控制器类

helloworldcontroller.java

package com.yapf.controller;
import org.springframework.stereotype.controller;
import org.springframework.ui.model;
import org.springframework.web.bind.annotation.requestmapping;
@controller
public class helloworldcontroller {
    @requestmapping("/hello")
    public string helloworld(model m) {
        string message = "hello world, spring mvc @ yapf";
        m.addattribute("message", message);
        return "hello"; 
    }
}

contactcontroller.java

package com.yapf.controller;
import org.springframework.stereotype.controller;
import org.springframework.ui.model;
import org.springframework.validation.bindingresult;
import org.springframework.web.bind.annotation.modelattribute;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.sessionattributes;
import com.yapf.form.contact;
@controller
@sessionattributes
public class contactcontroller {
    @requestmapping(value = "/addcontact", method = requestmethod.post)
    public string addcontact(@modelattribute("contact")  contact contact, bindingresult result) {
        //write the code here to add contact
        return "redirect:contact.html";
    }
    
    @requestmapping("/contact")
    public string showcontacts(model m) {
        m.addattribute("command", new contact());
        return "contact";
    }
}

 

4、在web.xml文件中提供控制器的条目

web.xml

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="webapp_id" version="2.5">
  <display-name>springtiles</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
            org.springframework.web.servlet.dispatcherservlet
        </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>
</web-app>

 

5、在xml文件中定义bean

spring-servlet.xml

<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"
    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-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.yapf.controller" />
<bean id="viewresolver" class="org.springframework.web.servlet.view.tiles3.tilesviewresolver"/>
<bean id="tilesconfigurer" class="org.springframework.web.servlet.view.tiles3.tilesconfigurer">
<property name="definitions">
<list>
<value>/web-inf/tiles.xml</value>
</list>
</property>
</bean>
</beans>

 

6、提供tile.xml文件

tiles.xml

<?xml version="1.0" encoding="utf-8" ?>
<!doctype tiles-definitions public
       "-//apache software foundation//dtd tiles configuration 2.0//en"
       "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
    <definition name="base.definition"
        template="/web-inf/jsp/layout.jsp">
        <put-attribute name="title" value="" />
        <put-attribute name="header" value="/web-inf/jsp/header.jsp" />
        <put-attribute name="menu" value="/web-inf/jsp/menu.jsp" />
        <put-attribute name="body" value="" />
        <put-attribute name="footer" value="/web-inf/jsp/footer.jsp" />
    </definition>
    <definition name="contact" extends="base.definition">
        <put-attribute name="title" value="contact manager" />
        <put-attribute name="body" value="/web-inf/jsp/contact.jsp" />
    </definition>
    <definition name="hello" extends="base.definition">
        <put-attribute name="title" value="hello spring mvc" />
        <put-attribute name="body" value="/web-inf/jsp/hello.jsp" />
    </definition>
</tiles-definitions>

 

7、创建请求的页面

index.jsp

<a href="hello.html">hello spring</a> | 
<a href="contact.html">contact</a>

 

8、创建其他视图组件

hello.jsp

<html>  
<head>  
    <title>spring mvc example</title>  
</head>  
<body>  
<h1>welcome to spring mvc</h1>  
    <p>message is: ${message}</p>  
</body>  
</html>  

contact.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>  
<html>  
<head>  
    <title>spring tiles contact form</title>  
</head>  
<body>  
<h2>contact manager</h2>  
<form:form method="post" action="addcontact.html">  
  
    <table>  
    <tr>  
        <td><form:label path="firstname">first name</form:label></td>  
        <td><form:input path="firstname" /></td>   
    </tr>  
    <tr>  
        <td><form:label path="lastname">last name</form:label></td>  
        <td><form:input path="lastname" /></td>  
    </tr>  
    <tr>  
        <td><form:label path="lastname">email</form:label></td>  
        <td><form:input path="email" /></td>  
    </tr>  
    <tr>  
        <td><form:label path="lastname">telephone</form:label></td>  
        <td><form:input path="telephone" /></td>  
    </tr>  
    <tr>  
        <td colspan="2">  
            <input type="submit" value="add contact"/>  
        </td>  
    </tr>  
</table>    
      
</form:form>  
</body>  
</html>

header.jsp

<h2>header</h2>  
<hr/>  

footer.jsp

<hr/>  
<p>copyright  2010-2014 yapf.com.</p>  

menu.jsp

<p>menu 1</p>  
<p>menu 2</p>  

layout.jsp

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>  
<!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><tiles:insertattribute name="title" ignore="true" /></title>  
</head>  
<body>  
        <div><tiles:insertattribute name="header" /></div>  
        <div style="float:left;padding:10px;width:15%;"><tiles:insertattribute name="menu" /></div>  
        <div style="float:left;padding:10px;width:80%;border-left:1px solid pink;">  
        <tiles:insertattribute name="body" /></div>  
        <div style="clear:both"><tiles:insertattribute name="footer" /></div>  
  
</body>  
</html>  

下一节:spring和jaxb集成的详细示例

spring 教程

相关文章