一、概述html
二、Action的做用java
三、第一種方式(設置私有屬性)web
log.jspapache
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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="login" method="post"> 用戶名:<input type="text" name="userName"/> 密碼:<input type="password" name="password"/> <input type="submit" value="提交"/> <input type="reset" value="重置"/> </form> </body> </html>
loginSuccess.jspjsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <!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> 登錄成功,歡迎你:<s:property value="userName"/> </body> </html>
LoginAction類:ide
package com.ljb.web.action; import com.opensymphony.xwork2.ActionSupport; /** * 第一種傳遞參數方法(設置私有屬性,設置set與get方法) * @author Administrator * */ public class LoginAction extends ActionSupport { 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; } @Override public String execute() throws Exception { // TODO Auto-generated method stub return SUCCESS; } }
struts.xmlpost
<?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> <!-- Add packages here --> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="login" class="com.ljb.web.action.LoginAction"> <result> /loginSuccess.jsp </result> </action> </package> </struts>
四、亂碼問題ui
login.jspthis
<%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%> <!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=gbk"> <title>Insert title here</title> </head> <body> <form action="login" method="post"> 用戶名:<input type="text" name="userName"/> 密碼:<input type="password" name="password"/> <input type="submit" value="提交"/> <input type="reset" value="重置"/> </form> </body> </html>
loginSuccess.jspspa
<%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%> <%@ taglib uri="/struts-tags" prefix="s" %> <!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=gbk"> <title>Insert title here</title> </head> <body> 登錄成功,歡迎你:<s:property value="userName"/> </body> </html>
緣由:
解決方法:
小結:
五、第二種方式(JavaBean)
建立JavaBean:
package com.ljb.entity; public class User { 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; } }
LoginAction2類:
package com.ljb.web.action; import com.ljb.entity.User; import com.opensymphony.xwork2.ActionSupport; public class LoginAction2 extends ActionSupport { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public String execute() throws Exception { // TODO Auto-generated method stub return SUCCESS; } }
login2.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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="login2" method="post"> 用戶名:<input type="text" name="user.userName"/> 密碼:<input type="password" name="user.password"/> <input type="submit" value="提交"/> <input type="reset" value="重置"/> </form> </body> </html>
loginSuccess2.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <!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> 登錄成功,歡迎你:<s:property value="user.userName"/> </body> </html>
struts.xml:
<?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> <!-- Add packages here --> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="login2" class="com.ljb.web.action.LoginAction2"> <result> /loginSuccess2.jsp </result> </action> </package> </struts>
操做小技巧(xml中添加提示功能):
小結:
六、第三種方式(ModelDriven)
LoginAction3.jsp:
package com.ljb.web.action; import com.ljb.entity.User; import com.opensymphony.xwork2.ModelDriven; public class LoginAction3 implements ModelDriven { private User user = new User(); @Override public User getModel() { // TODO Auto-generated method stub return user; } public String execute() { return "success"; } }
注:其他頁面參數跟第一種方式相同
七、小結