Struts2接受數據的三種方式

  怎麼說呢,今天是接觸Struts2的第二天了。關於Struts2來說,也不能說難。只能說複雜。也難怪,WEB這個方向能龐大,裏面的東西能不復雜嗎。但話又說回來,複雜也有其複雜的好處。比如,

jsp+javaBean簡單,但那是往小了說,如果項目足夠龐大,Struts2 的好處就顯現出來了。產品不是做出來就算牛逼了。關鍵還是在於後期的維護,如果項目的擴展性很好,耦合度很小,那麼後期的維護 也便方便些。所以不管怎麼講,再複雜也得研究,也得學。只要不是笨到只會左腳邁步的人,什麼東西都是能學會的。廢話也不多講了。

關於Struts2的學習,只要是對大家有好處的東西,我都會及時寫博客的,望大家一塊學習,一塊進步。

a、  使用領域對象接受用戶輸入:input接受參數的名字要命成領域對象.名稱(user.username, user.password)。原理:通過Struts2框架的數據綁定機制,傳遞user.username請求參數等同於調用:action.getUser().setUsername(…)

實例:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.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>
    <form action="login.action" method="post">
    	<table>
    		<tr>
    		<td>
    			Username:<input type="text" name="user.username" />
    		</td>
    		</tr>
    		<tr>
    			<td>
    				Password:<input type="password" name="user.password" />
    			</td>
    		</tr>
    		<tr>
    			<td>
    				<input type="submit" value="Save">
    			</td>
    		</tr>
    	</table>
    </form>
  </body>
</html>
User.java:

public class User {

	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;
	}
	private String username;
	private String password;
	
}
LoginAction.java:
import com.opensymphony.xwork2.Action;


public class LoginAction implements Action{

	private User user;
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public String execute() throws Exception {
		if("cuijun".equals(user.getUsername()) && "123".equals(user.getPassword())){
			return SUCCESS;
		}
		return ERROR;
	}

}


 

Success.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%@taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.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>
    <h3><s:property value="user.username" /></h3>
  </body>
</html>
error.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.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>
    用戶名和密碼錯誤,請重新登錄<a href="login.jsp">
  </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>
  
 <package  name="default" extends="struts-default">
 	<action name="login" class="LoginAction">
 		<result>/success.jsp</result>
 		<result name="error">/error.jsp</result>
 	</action>
 </package>
</struts>


 

a、直接使用action的屬性接受用戶輸入。也就是把<input>裏的user.username和user.password轉換成username和password。而在LoginAction.java中直接定義username和password屬性。不再使用User。同時也要生成username和password的setter和getter方法。

b、使用ModelDriven action。只需把修改LoginAction.java,如下:

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;


public class LoginAction implements Action, ModelDriven{

	private User user = new User();
	
	public String execute() throws Exception {
		if("cuijun".equals(user.getUsername()) && "123".equals(user.getPassword())){
			return SUCCESS;
		}
		return ERROR;
	}
	
	public Object getModel() {
		// TODO Auto-generated method stub
		return user;
	}
}


 

LoginAction實現了ModelDriven接口,表單字段就不需要再使用“user.”前綴了。


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章