structs输入校验实例代码如下 webxml <?xml version= encoding=UTF?> <webapp version= xmlns=/xml/ns/javaee xmlns:xsi=//XMLSchemainstance xsi:schemaLocation=/xml/ns/javaee /xml/ns/javaee/webapp__xsd> <filter> <filtername>struts</filtername> <filterclass>orgapachestrutsdispatcherFilterDispatcher</filterclass> </filter> <filtermapping> <filtername>struts</filtername> <urlpattern>/*</urlpattern> </filtermapping> </webapp> strutsxml <?xml version= encoding=GBK ?> <!DOCTYPE struts PUBLIC //Apache Software Foundation//DTD Struts Configuration //EN /dtds/strutsdtd> <struts> <package name=struts extends=strutsdefault> <action name=Login class=HelloWorldLoginAction> <result name=input>/helloworld/indexjsp</result> <result name=success>/helloworld/welcomejsp</result> <result name=error>/helloworld/errorjsp</result> </action> </package> </struts> LoginAction package HelloWorld; import comopensymphonyxworkAction; import comopensymphonyxworkActionContext; import comopensymphonyxworkActionSupport; public class LoginAction extends ActionSupport…{ private String username; private String password; private String[] books; public String[] getBooks() …{ return books; } public void setBooks(String[] books) …{ thisbooks = books; } public String execute() throws Exception…{ if(thisgetUsername()equals(admin)&&thisgetPassword()equals())…{ ActionContextgetContext()getSession()put(user thisgetUsername()) BookService bs=new BookService() thissetBooks(bsgetBooks()) return SUCCESS; }else…{ return ERROR; } } public String getUsername() …{ return username; } public void setUsername(String username) …{ thisusername = username; } public String getPassword() …{ return password; } public void setPassword(String password) …{ thispassword = password; } } BookServicejava package HelloWorld; public class BookService …{ private String[] books=new String[]…{ Spring Hibernate Struts }; public String[] getBooks()…{ return books; } } LoginActionvalidationxml <?xml version= encoding=GBK?> <!DOCTYPE validators PUBLIC //OpenSymphony Group//XWork Validator //EN /xwork/xworkvalidatordtd> <validators> <field name=username> <fieldvalidator type=requiredstring> <message key=userrequired/> </fieldvalidator> </field> <field name=password> <fieldvalidator type=requiredstring> <message key=passrequired/> </fieldvalidator> </field> </validators> loginjsp <%…@ page language=java contentType=text/html; charset=gb pageEncoding=gb%> <%…@ taglib prefix=s uri=/strutstags %> <html> <head> <title></title> </head> <body> <s:form action=Login> <s:textfield name=username key=username></s:textfield> <s:password name=password key=password></s:password> <s:submit value=login></s:submit> </s:form> </body> </html> welcomejsp <%…@ page language=java contentType=text/html; charset=gb pageEncoding=gb%> <%…@ page isELIgnored=false %> <%…@ taglib prefix=s uri=/strutstags %> <%…@ taglib prefix=c uri=/jsp/jstl/core %> <html> <head> <title></title> </head> <body> 欢迎${sessionScopeuser}登陆 <c:out value=${requestScopenow}></c:out> <br> <table border=> <s:iterator value=books status=index> <s:if test=#indexodd==true> <tr style=backgroundcolor=#cccccc> </s:if> <s:else> <tr> </s:else> <td>书名</td> <td><s:property/></td> </tr> </s:iterator> </table> </body> </html> errorjsp <%…@ page language=java contentType=text/html; charset=gb pageEncoding=gb%> <html> <head> <title></title> </head> <body> 登陆失败 </body> </html> |