Bạn có thể định nghĩa nhiều namespace trong struts.xml file bởi thuộc tính namespace của phần tử package. Như bạn đã biết, namespace mặc định là / (root).

Ví dụ đơn giản minh họa việc định nghĩa nhiều namespace trong struts.xml file.

Lúc này, struts.xml file bao gồm ba package với các name và namespace khác nhau.

struts.xml

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

<struts>

<package name="default1" namespace="/" extends="struts-default">
<action name="hello"  class="com.hoclaptrinh.Welcome">
<result>welcome.jsp</result>
</action>
</package>

<package name="default2" namespace="/first" extends="struts-default">
<action name="hello"  class="com.hoclaptrinh.Welcome">
<result>welcome.jsp</result>
</action>
</package>

<package name="default3" namespace="/second" extends="struts-default">
<action name="hello"  class="com.hoclaptrinh.Welcome">
<result>welcome.jsp</result>
</action>
</package>

</struts>  

Các resource cần thiết khác

Chúng ta cần một số file khác để hiểu toàn bộ ví dụ về việc sử dụng nhiều namespace này, đó là index.jsp, lớp action (welcome.java) và ba thành phần view.

Thứ nhất tạo index.jsp: JSP page này tạo ba link.

<a href="hello">root namespace</a>|
<a href="first/hello">first namespace</a>|
<a href="second/hello">second namespace</a>

Thứ hai, tạo action class: Lớp Action đơn giản chỉ chứa phương thức view.

welcome.java

package com.hoclaptrinh;

public class Welcome {
public String execute(){
    return "success";
}
}

Ba thành phần view

Tại đây, tên của cả ba thành phần view là giống nhau nhưng chúng có vị trí khác nhau.

welcome.jsp: Nó phải được đặt trong root directory.

<h1>Welcome to root namespace</h1>

welcome.jsp: Nó được đặt bên trong thư mục đầu tiên dưới root.

<h1>Welcome to first namespace</h1>

welcome.jsp: Nó được đặt bên trong thư mục thứ hai dưới root.

<h1>Welcome to second namespace</h1>

Viết câu trả lời

Drop Images

0 Bình luận