<?xml version="1.0" encoding="UTF-8" ?> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Insert title here</title> </head> <body> <form action="login.action" method="post"> username:<input type="text" name="username" /><p> password:<input type="password" name="password" /><p> <input type="submit" name="Submit" value="提交" /> </form> </body> </html>
<?xml version="1.0" encoding="UTF-8" ?> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Insert title here</title> </head> <body> username:${requestScope.username}<br> password:${requestScope.password} </body> </html>
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name> struts2.0</display-name> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2.0</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2.0</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
package com.action; public class loginAction { private String username; private String password; 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; } public String execute()throws Exception{ return "success"; } }
<?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.0.dtd"> <struts> <package name="struts2.0" extends="struts-default"> <action name="login" class="com.action.loginAction"> <result name="success">/result.jsp</result> </action> </package> </struts>
<s:form action="login"> <s:textfield name="username" label="username"></s:textfield> <s:password name="password" label="password"></s:password> <s:submit label="submit"></s:submit> </s:form>
@Override public void validate() { if(null==this.getUsername()||"".equals(this.getUsername().trim())){ this.addFieldError("username", "username error");//添加错误信息 } if(null==this.getPassword()||"".equals(this.getPassword().trim())){ this.addFieldError("password", "password error");//添加错误信息 } }
<?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.0.dtd"> <struts> <package name="struts2.0" extends="struts-default"> <action name="login" class="com.action.loginAction"> <!-- 添加出错返回的页面 --> <result name="input">/login.jsp</result> <result name="success">/result.jsp</result> </action> </package> </struts>
public String execute()throws Exception{ if("hello".equals(this.getUsername().trim())&&"world".equals(this.getPassword().trim())){ return "success"; } else{ this.addFieldError("username", "username or password error");//定义在username上面提示错误信息 return "failer"; } }
<?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.0.dtd"> <struts> <package name="struts2.0" extends="struts-default"> <action name="login" class="com.action.loginAction"> <result name="input">/login.jsp</result> <result name="success">/result.jsp</result> <!-- 增加failer映射 --> <result name="failer">/login.jsp</result> </action> </package> </struts>