这个是我第一次发帖,没有经验,自己一在校学生,自己也是刚学struts2几天,这个纯粹是为了入门,所以验证类型只是随意,大家看懂了这个我想对于其他的验证类型也小菜了吧!
至于struts自带的标签,我不喜欢用,因为自带了theme,但是用el也会很麻烦,有“["这个符号。。。
恳请哪位懂得如何拿到他的plain text 的教一下,感激不尽!
package cn.longkai.validate; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { private static final long serialVersionUID = 1L; private String userName; private String password; @Override public String execute() throws Exception { return super.execute(); } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <validators> <field name="userName"> <field-validator type="requiredstring"> <message>user name is required!!!</message> </field-validator> </field> <field name="password"> <field-validator type="requiredstring"> <message>password is required!!!</message> </field-validator> </field> </validators>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="test" namespace="/test" extends="struts-default"> <action name="validate" class="cn.longkai.validate.LoginAction"> <result>/success.jsp</result> <result name="input">/error.jsp</result> </action> </package> </struts>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="test/validate" method="post"> <input type="text" name="userName" /> <input type="text" name="password" /> <input type="submit" value="submit" /> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> ${errors.userName } <s:fielderror /> <s:debug /> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> login success! <s:debug /> </body> </html>