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 blah.do.
– 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
scope.
– 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 :
struts-config.xml
</pre><br /><action-mappings><br /><br /><action path="/register" type="thaihoanghai.RegisterAction"><br /><br /><forward name="success" path="/WEB-INF/result/confirm.jsp"></forward><br /><br /><forward name="password-invalid" path="/WEB-INF/result/password-invalid.jsp"></forward><br /><br /><forward name="username-invalid" path="/WEB-INF/result/username-invalid.jsp"></forward><br /><br /></action><br /><br /></action-mappings><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.
<global-forwards>
.<forward name=”success” path=”/WEB-INF/results/confirm.jsp”/>
</global-forwards>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 /><%@ page language="java" contentType="text/html; charset=ISO-8859-1"<br />pageEncoding="ISO-8859-1"%><br /><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><br /><html><br /><head><br /><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><br /><title>Registration</title><br /></head><br /><body><br /><h1>New Account Registration</h1><br /><form action="register.do" method="post"><br /><%%KEEPWHITESPACE%%> UserName : <input type="text" name="username"><br/><br /><%%KEEPWHITESPACE%%> Password : <input type="password" name="password"><br/><br /><%%KEEPWHITESPACE%%> <input type="submit" value="Sign Me Up!"><br /></form><br /></body><br /></html><br />Mô tả về cách hoạt đông :
Leave a comment