📅  最后修改于: 2020-12-04 08:47:59             🧑  作者: Mango
Spring提供了与apache tile框架的集成支持。因此,我们可以借助Spring Tile支持简单地管理Spring MVC应用程序的布局。
可重用性:我们可以在多个页面中重用单个组件,例如页眉和页脚组件。
集中控制:我们只能通过单个模板页面来控制页面的布局。
易于更改布局:借助单个模板页面,我们可以随时更改页面的布局。因此,您的网站可以轻松采用新技术,例如引导程序,jQuery等。
让我们在eclipse中查看spring tile示例的文件。
pom.xml
org.springframework
spring-webmvc
5.1.1.RELEASE
javax.servlet
servlet-api
3.0-alpha-1
javax.servlet
jstl
1.2
org.apache.tomcat
tomcat-jasper
9.0.12
org.apache.tiles
tiles-jsp
3.0.5
org.apache.tiles
tiles-servlet
3.0.5
org.apache.tiles
tiles-core
3.0.5
org.apache.tiles
tiles-el
3.0.5
Contact.java
package com.javatpoint.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;
}
}
HelloWorldController.java
package com.javatpoint.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 @ Javatpoint";
m.addAttribute("message", message);
return "hello";
}
}
ContactController.java
package com.javatpoint.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.javatpoint.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";
}
}
web.xml
SpringTiles
index.jsp
spring
org.springframework.web.servlet.DispatcherServlet
1
spring
*.html
spring-servlet.xml
/WEB-INF/tiles.xml
Tiles.xml
index.jsp
hello.jsp
Spring MVC Example
Welcome to Spring MVC
Message is: ${message}
contact.jsp
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
Spring Tiles Contact Form
Contact Manager
First Name
Last Name
Email
Telephone
header.jsp
Header
footer.jsp
Copyright 2010-2014 javatpoint.com.
menu.jsp
Menu 1
Menu 2
layout.jsp
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
输出:
如果您不使用maven,请下载tile jar。