項目網站:www.aikan66.com
遊戲網站:www.aikan66.com
圖片網站:www.aikan66.com
書籍網站:www.aikan66.com
學習網站:www.aikan66.com
Java網站:www.aikan66.com
html
----web
應用Struts驗證框架並對用戶登陸頁面進行輸入驗證。apache
----框架
一、配置Struts2環境(web08),web.xml中註冊過濾器。jsp
二、UserAction類ide
package dog; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport{ private static final long serialVersionUID=1L; private String username; private String password; //用戶登陸 @Override public String execute() throws Exception{ return SUCCESS; } 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; } }
三、struts.xml配置post
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 開發模式打開 --> <constant name="struts.devMdoe" value="true"/> <!-- 聲明包 --> <package name="myPackage" extends="struts-default"> <!-- 定義action --> <action name="userAction" class="dog.UserAction"> <!-- 用戶登陸頁面 --> <result name="input">/login.jsp</result> <!-- 定義成功的映射頁面 --> <result>/success.jsp</result> </action> </package> </struts>
四、建立login.jsp學習
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <s:form action="userAction" methode="post"> <s:textfield name="username" label="用戶名" required="true"></s:textfield> <s:password name="password" label="密碼" required="true"></s:password> <s:submit key="submit" value="登陸"></s:submit> </s:form> </body> </html>
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'success.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <s:property value="username"/>,登陸成功 </body> </html>
五、編寫用戶登陸驗證文件,UserAction-validation.xml,必須放在UserAction.java中。
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
<!-- 驗證用戶名 -->
<field name="username">
<field-validator type="requiredstring">
<message>請輸入用戶名</message>
</field-validator>
</field>
<!-- 驗證密碼 -->
<field name="password">
<field-validator type="requiredstring">
<message>請輸入密碼</message>
</field-validator>
</field>
</validators>
六、部署,訪問:localhost:8080/jwrm157-yanzheng/login.jsp
點擊登陸
----
完畢