Struct Action Basic

Bước qua bài đầu tiên chúng ta cần nắm các kiến thức như sau :

  • The user requests a form

-For now we use normal HTML to build the form
• Later we will use the Struts html:form tag

  • The form is submitted to a URL of the form

– That address is mapped by struts-config xmltoanActionclass

  • The execute method of the Action object is invoked

– One of the arguments to execute is a form bean that is automatically
created and whose properties are automatically populated with the created and whose properties are automatically populated with the
incoming form data
– The Action object then invokes business logic and data-access logic,
placing the results in normal beans stored in request, session, or application
– The Action uses mapping.findForward to return a condition, and the
conditions are mapped by struts-config.xml to various JSP pages.

  • Struts forwards request to the appropriate JSP page • Struts forwards request to the appropriate JSP page

– The page can use bean:write or the JSP 2.0 EL to output bean properties
– The page can use bean:message to output fixed strings

Source code : Download

Cấu trúc thư mục như sau :



&lt;/pre&gt;<br />&lt;action-mappings&gt;<br /><br />&lt;action path="/register" type="thaihoanghai.RegisterAction"&gt;<br /><br />&lt;forward name="success" path="/WEB-INF/result/confirm.jsp"&gt;&lt;/forward&gt;<br /><br />&lt;forward name="password-invalid" path="/WEB-INF/result/password-invalid.jsp"&gt;&lt;/forward&gt;<br /><br />&lt;forward name="username-invalid" path="/WEB-INF/result/username-invalid.jsp"&gt;&lt;/forward&gt;<br /><br />&lt;/action&gt;<br /><br />&lt;/action-mappings&gt;<br /><br />

Note: if the same forward is used by multiple actions, you can put the forward declaration in a global-forwards section (before action-mappings )  instead of in the aciton.

    .<forward name=”success” path=”/WEB-INF/results/confirm.jsp”/>

File Action của mình :

<br />package thaihoanghai;<br /><br />import javax.servlet.http.HttpServletRequest;<br />import javax.servlet.http.HttpServletResponse;<br /><br />import org.apache.struts.action.Action;<br />import org.apache.struts.action.ActionForm;<br />import org.apache.struts.action.ActionForward;<br />import org.apache.struts.action.ActionMapping;<br /><br />public class RegisterAction extends Action{<br /><%%KEEPWHITESPACE%%>	@Override<br /><%%KEEPWHITESPACE%%>	public ActionForward execute(ActionMapping mapping, ActionForm form,<br /><%%KEEPWHITESPACE%%>			HttpServletRequest request, HttpServletResponse response) throws Exception {<br /><%%KEEPWHITESPACE%%>		// TODO Auto-generated method stub<br /><br /><%%KEEPWHITESPACE%%>		String username = request.getParameter("username");<br /><%%KEEPWHITESPACE%%>		String password = request.getParameter("password");<br /><br /><%%KEEPWHITESPACE%%>		if(username == null || username.trim().length() == 0)<br /><%%KEEPWHITESPACE%%>			return (mapping.findForward("username-invalid"));<br /><%%KEEPWHITESPACE%%>		else if (password == null || password.trim().length() == 0)<br /><%%KEEPWHITESPACE%%>			return (mapping.findForward("password-invalid"));<br /><%%KEEPWHITESPACE%%>		else<br /><%%KEEPWHITESPACE%%>			return (mapping.findForward("success"));<br /><br /><%%KEEPWHITESPACE%%>	}<br />}<br />

Trang index của chúng ta

<br />&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"<br />pageEncoding="ISO-8859-1"%&gt;<br />&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""&gt;<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;<br />&lt;title&gt;Registration&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&lt;h1&gt;New Account Registration&lt;/h1&gt;<br />&lt;form action="" method="post"&gt;<br /><%%KEEPWHITESPACE%%>	UserName : &lt;input type="text" name="username"&gt;&lt;br/&gt;<br /><%%KEEPWHITESPACE%%>	Password : &lt;input type="password" name="password"&gt;&lt;br/&gt;<br /><%%KEEPWHITESPACE%%>	&lt;input type="submit" value="Sign Me Up!"&gt;<br />&lt;/form&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br />

Mô tả về cách hoạt đông :



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: