Giải pháp là thêm phụ thuộc bắt buộc, tải các lát gạch với trình nghe thích hợp và tạo loại kết quả tùy chỉnh. May mắn thay, các bước này khá dễ dàng, một khi đã hoàn thành, bạn có thể làm theo các lát bình thường 2 ví dụ để biết cách xác định các mẫu.
1) Dependencies (bắt đầu với dự án thanh chống cơ bản nhưng trong ví dụ này, tôi sẽ sử dụng công ước nên nó chỉ có thể là tốt nhất để thêm struts2-ước-plugin, nó sẽ bao gồm struts2 lõi et al):
- KHÔNG BAO GỒM NHỮNG SỰ struts2-gạch-plugin
- groupId: org.apache.tiles, artifiactId: gạch-extras, phiên bản: 3.0.1
- groupId: org.slf4j, artifiactId: JCL-over-slf4j, phiên bản: 1.5.8
- groupId: org.slf4j, artifiactId: slf4j-jdk14, phiên bản : 1.5.8
Note: Một phiên bản cao hơn của sự phụ thuộc slf4j thể công việc tôi chưa thử nghiệm điều này.
2) gạch tải với một người nghe thích hợp
Ví dụ này bao gồm đầy đủ các web.xml, dòng 3-5 là điều duy nhất mà nên mới để một người nào đó quen thuộc với struts2.
<?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">
<listener>
<listener-class>org.apache.tiles.extras.complete.CompleteAutoloadTilesListener</listener-class>
</listener>
<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>
</web-app>
3) tạo ra một loại quả tùy chỉnh
Chúng ta cần phải xác định một loại quả tùy chỉnh để sử dụng với những hành động của chúng tôi:
package com.quaternion.result;
import com.opensymphony.xwork2.ActionInvocation;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.ServletDispatcherResult;
import org.apache.tiles.TilesContainer;
import org.apache.tiles.access.TilesAccess;
import org.apache.tiles.request.ApplicationContext;
import org.apache.tiles.request.servlet.ServletRequest;
import org.apache.tiles.request.servlet.ServletUtil;
public class TilesResult extends ServletDispatcherResult {
public TilesResult() {
super();
}
public TilesResult(String location) {
super(location);
}
@Override
public void doExecute(String location, ActionInvocation invocation) throws Exception {
//location = "test.definition"; //for test
setLocation(location);
ServletContext context = ServletActionContext.getServletContext();
ApplicationContext applicationContext = ServletUtil.getApplicationContext(context);
TilesContainer container = TilesAccess.getContainer(applicationContext);
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
ServletRequest servletRequest = new ServletRequest(applicationContext, request, response);
container.render(location, servletRequest);
}
}
4) Chúng tôi cũng cần phải nói cho struts2 về loại kết quả của chúng tôi:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.ui.theme" value="simple" />
<package name="tiles-package" namespace="" extends="struts-default">
<result-types>
<result-type default="true" name="tiles-result" class="com.quaternion.result.TilesResult"/>
</result-types>
</package>
</struts>
Với điều đó ra khỏi wa Bây giờ chúng ta có thể sử dụng các tile trong các dự án của chúng ta, giả sử chúng ta đã tạo ra một định nghĩa lát gạch gọi là "test".định nghĩa" chúng ta có thể nói hành động của chúng tôi để sử dụng xác định này bằng cách làm như sau:
package com.quaternion.demo.action.test;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
@ParentPackage("tiles-package")
@Result(type="tiles-result", location="test.definition")
public class QuaternionResultTest extends ActionSupport{}
Vậy đó, điều này sẽ cho phép bạn cấu hình bất kỳ phiên bản của struts2 với gạch 3+, vui lòng xem http://tiles.apache.org/framework/index.html để biết chi tiết cấu hình thêm .
Tôi đã rối tung xung quanh bằng cách buộc gạch vào công thức struts2, Nó đang hoạt động, nhưng nó không phải là đẹp ... có lẽ tôi có thể gửi điều đó quá bạn? – Quaternion
Xin vui lòng làm, đó là nỗ lực đầu tiên để hỗ trợ Gạch 3, vì vậy chúng tôi có thể mở rộng nó với bản phát hành tiếp theo –
Đã chuyển câu trả lời được chấp nhận ở đây vì ... plugin này thực hiện ở trên với công việc ít hơn nhiều. – Quaternion