Sử dụng Struts trong java

Đăng bởi admin lúc November 1st, 2009 - trong chuyên mục Lập trình

Cách để làm việc nhẹ nhàng hơn với nhập liệu trong dịch vụ web của JSP.

Thư viện:

•Tải về từ địa chỉ sau:

http://www.mediafire.com/download.php?izk3yht5nyo

Bao gồm (tối thiểu để chạy struts):

Photobucket

Cách cài đặt: file struts

<!DOCTYPE struts PUBLIC

“-//Apache Software Foundation//DTD Struts Configuration 2.0//EN”

“http://struts.apache.org/dtds/struts-2.0.dtd”>

<struts>

<package name=”default” extends=”struts-default”>

<action name=”HelloWorld”>

<result name=”SUCCESS”>/success.jsp</result>

</action>

</package>

</struts>

File này nằm trong thư mục gốc của thư mục src ( thư mục chứa file java)

Cách cài đặt: thêm vào file web.xml

<filter>

<filter-name>struts2</filter-name>

<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

</filter>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

Cách sử dụng: form struts

Khai báo taglib như thẻ c (jstl)

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

Ví dụ:

<s:form action=”HelloWorld” >

<s:textfield name=”userName” label=”User Name” />

<s:submit />

</s:form>

Có thể kô sử dụng taglib, nhưng trong form, thẻ action thì bắt buộc tên action có thêm đuôi .action

Ví dụ:

Thay <form action=“Hello” … >

Thành <form action=“Hello.action” …>

Cách sử dụng: lớp action

Tạo mới lớp java bình thường: Biến nhận vào phải giống với name trong input ở thẻ form.

Biến phải có đầy đủ getter và setter. Chỉ được thao tác với biến qua getter và setter.

Ví dụ:

Biến String name;

Chỉ được gọi getName() kô được dùng name

Hiện thực phương thức:

public String execute() {};

Sẽ viết sử lý của lớp action trong đây.

Trả về có 2 mặc định là:

Return SUCCESS; (nếu đúng)

Return INPUT; (nếu có sai sót)

Cấu hình: file struts

Mỗi action đều fải khai báo với file struts

Thêm vào:

<action name=”HelloWorld”>

<result name=”SUCCESS”>/success.jsp</result>

</action>

Tương ứng với trả về trong file action thì sẽ trỏ địa chỉ trang trả về.

Xem ví dụ ở: http://www.vaannila.com/struts-2/struts-2-example/struts-2-example.html

Tags: ,

5 Responses

  1. canary says:

    ùm,thank a đã post bài nì

  2. noName() says:

    Bai viet qua so sai, neu da viet thi viet no chi tiet mot chut

  3. List nhạc hot của bạn hay ghê. Nghe đi nghe lại mấy lần không chán .

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© Mr.Cam update from iFeature(Cyberchimps)