一个login form的完整例子将展示Struts 如何简单的处理forms。请查看下面的页面logon.jsp:
<%@ page language="java" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <html:html> <head> <title> <bean:message key="logon.title"/> </title> </head> <body bgcolor="white"> <html:errors/> <html:form action="/logon" focus="username"> <table border="0" width="100%"> <tr> <th align="right"> <bean:message key="prompt.username"/> </th> <td align="left"> <html:text property="username" size="16"/> </td> </tr> <tr> <th align="right"> <bean:message key="prompt.password"/> </th> <td align="left"> <html:password property="password" size="16"/> </td> </tr> <tr> <td align="right"> <html:submit> <bean:message key="button.submit"/> </html:submit> </td> <td align="right"> <html:reset> <bean:message key="button.reset"/> </html:reset> </td> </tr> </table> </html:form> </body> </html:html>
下面的条目展示了在Struts中form的主要特征:
· taglib 告诉JSP 编译器到哪里去寻找Struts tag library 的tag library descriptor 。在本例中,我们使用bean 作为struts-bean库的前缀,使用html 作为struts-html 库的前缀。任何其他的前缀也可以被使用。
· 本页面使用了message tag 去寻找internationalized message strings 。为了使这个页面正常运行,下列message keys 必须在资源中定义:
o logon.title - logon 页面的标题
o prompt.username – 一个"Username:" 提示字符串
o prompt.password – 一个"Password:" 提示字符串
o button.submit – 按钮标签"Submit"
o button.reset – 按钮标签"Reset"
当用户登录时,应用程序可以在用户会话中保存一个Locale 对象。这个Locale 对象将被使用来选择合适的语言对应的资源。这使得实现一个用户选择切换语言的功能变得十分的容易—简单的改变Locale 对象,所有的messages 将自动的切换。
· The errors tag 显示任何被商业逻辑组件保存的错误信息,如果没有错误被保存,那么就什么也不显示。该tag 在后面将会被描述。
· The form tag renders an HTML <form> element, based on the specified attributes. It also associates all of the fields within this form with a session scoped FormBean that is stored under the key logonForm. The Struts developer provides the Java implementation of this form bean, subclassing the Struts class ActionForm. This bean is used to provide initial values for all of the input fields that have names matching the property names of the bean. If an appropriate bean is not found, a new one will be created automatically, using the specified Java class name.
· The form bean can also be specified in the Struts configuration file, in which case the Name and Type can be omitted here. See "The Action Mappings Configuration File" for details.)
· The text tag renders an HTML <input> element of type "text". In this case, the number of character positions to occupy on the browser's screen has been specified as well. When this page is executed, the current value of the username property of the corresponding bean (that is, the value returned by getUsername()).
· The password tag is used similarly. The difference is that the browser will echo asterisk characters, instead of the input value, as the user types their password..
· The submit and reset tags generate the corresponding buttons at the bottom of the form. The text labels for each button are created using message tags, as with the prompts, so that these values are internationalized.
处理multipart forms 也很容易。很明显,当你创建一个multipart form 时,你正在创建一个至少包含了一个“file”类型input的form。
<%@page language="java"> <%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"> <html:form action="uploadAction.do" enctype="multipart/form-data"> Please Input Text: <html:text property="myText"><br/> Please Input The File You Wish to Upload:<br/> <html:file property="myFile"><br /> <html:submit /> </html:form>
下一步是创建你的ActionForm bean:
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.upload.FormFile; public class UploadForm extends ActionForm { protected String myText; protected FormFile myFile; public void setMyText(String text) { myText = text; } public String getMyText() { return myText; } public void setMyFile(FormFile file) { myFile = file; } public FormFile getMyFile() { return myFile; } }
参见FormFile的JavaDocs。同时参见ActionServlet和ActionMapping的JavaDocs,看看你可以指定哪些参数。最基本的,是在你action 类的peform() 方法中调用((UploadForm) form).getMyFile() 来取得FormFile,并作处理。 |