Bạn có thể tùy chỉnh Layout của ứng dụng Struts 2 bởi tích hợp với Tiles Framework. Một webpage có thể gồm nhiều phần (được biết như là các tile) như header, cạnh trái, cạnh phải, phần thân, phần footer, … Trong Tiles Framework, chúng ta quản lý tất cả các tile bởi Layout Manager của chúng ta.

Các bước để tạo ứng dụng Tiles:

  • Thêm tiles library vào trong ứng dụng của bạn.
  • Định nghĩa Struts2TilesListener trong web.xml file
  • Tạo index.jsp
  • Tạo lớp action
  • Kế thừa tiles-default package trong package của bạn và định nghĩa tất cả result type dưới dạng các tile trong struts.xml file.
  • Tạo tiles.xml file và định nghĩa tất cả các tile.
  • Tạo LayoutManager page.
  • Tạo các thành phần View.

1. Thêm tiles library vào trong ứng dụng của bạn

Nếu bạn đang sử dụng myeclipse IDE, bạn có thể thêm tiles library bởi nhấn chuột phải vào project > Build Path > Add Library > Add Myeclipse Library > Select the Struts 2 tiles library > ok.

2. Định nghĩa Struts2TilesListener trong web.xml file

Cung cấp entry của lớp Struts2TilesListener trong web.xml file.

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">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
   <filter-name>struts2</filter-name>
   <filter-class>
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
   </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <listener>
  <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
  </listener>

 </web-app>

3. Tạo index.jsp

<%@ taglib uri="/struts-tags" prefix="s" %>

<s:form action="login">
<s:textfield name="name" label="Name"></s:textfield>
<s:password name="password" label="Password"></s:password>
<s:submit value="login"></s:submit>
</s:form>

4. Tạo lớp action

Lớp action này chứa một tên trường và định nghĩa phương thức execute.

Login.java

package com.hoclaptrinh;

public class Login {
private String name,password;

//phuong thuc getter va setter

public String execute(){
if(password.equals("admin")){
    return "success";
}
else{
    return "error";
}
}
}

5. Kế thừa tiles-default package trong package của bạn và định nghĩa tất cả result type dưới dạng các tile trong struts.xml file

xml file này định nghĩa một package với một action và hai result.

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>

<struts>

<package name="abc" extends="tiles-default" >

<action name="login" class="com.hoclaptrinh.Login">
<result name="success" type="tiles">login-success</result>
<result name="error" type="tiles">login-error</result>
</action>

</package>
</struts> 

6. Tạo tiles.xml file và định nghĩa tất cả các tile

tiles.xml file phải được đặt bên trong thư mục WEB-INF.

tiles.xml

<?xml version="1.0" encoding="UTF-8" ?> 

<tiles-definitions>

   <definition name="login-success" template="/layoutmanager.jsp"> 
   <put-attribute name="title" value="Welcome Page"/> 
   <put-attribute name="body" value="/login-success.jsp"/> 
   </definition>

   <definition name="login-error" template="/layoutmanager.jsp"> 
   <put-attribute name="title" value="Login Error"/> 
   <put-attribute name="body" value="/login-error.jsp"/> 
   </definition> 

</tiles-definitions>

7. Tạo LayoutManager page

Nó sử dụng thẻ getAsString của Tiles để bao string resource và thẻ insertAttribute của Tiles để bao page resource.

layoutmanager.jsp

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>

<html>
<head>
<title><tiles:getAsString name="title" /></title>
</head>
<body>

<%@  include file="header.jsp" %>
<tiles:insertAttribute name="body" />
<%@ include file="footer.jsp" %>

</body>
</html> 

8. Tạo các thành phần View

Có nhiều thành phần view như header.jsp, footer.jsp, welcome.jsp, …

header.jsp

<h2 style="background-color:pink;text-align:center;">It is header tile</h2>
<hr/>

footer.jsp

<hr>
<h2 style="background-color:pink;text-align:center;">It is footer tile</h2>

login-success.jsp

<%@ taglib uri="/struts-tags" prefix="s" %>

Welcome, <s:property value="name"/>

login-error.jsp

Xin loi, xay ra loi voi username hoac password ban nhap vao!
<jsp:include page="index.jsp"></jsp:include>

Cách định nghĩa nhiều tiles file trong ứng dụng Struts 2

Để định nghĩa nhiều tiles, bạn cần thêm entry sau trong web.xml file.

<context-param id="struts_tiles">
 <param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
 <param-value>/WEB-INF/tiles1.xml,/WEB-INF/tiles2.xml</param-value>
</context-param>

Viết câu trả lời

Drop Images

0 Bình luận