Struts2 HelloWorld之後乾點實事吧!

呵呵~一鼓作氣!再弄點!HelloWorld不能一直下去吧!

HelloWorld裏的struts.xml很簡單。

  1.  <package name="default" extends="struts-default">  
  2.         <action name="hello">  
  3.             <result >  
  4.                 helloworld.jsp   
  5.             </result>  
  6.         </action>  
  7.     </package>  

 

 
就是簡單的判斷當前的URL是否爲/hello,如果是,則直接返回結果,在web中,結果的輸出就是展現在瀏覽器中的頁面!這裏就是helloworld.jsp。此時,沒有對輸入進行任何操作。
 
我們就以最簡單的用戶登錄作爲例子。
首先創建一個Action。
 
package com.hello.model;

import com.opensymphony.xwork2.ActionSupport;

public class UserLoginAction extends ActionSupport {

	private String userName ;
	private String password ;
	
	public String getUserName() {
		System.out.println("getUserName");
		return userName;
	}
	public void setUserName(String userName) {
		System.out.println("setUserName");
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String add() {
		System.out.println(userName + " has been accepted!");
		return ActionSupport.SUCCESS;
	}
}
 
裏面的輸出方便觀察調用時機,只用看userName就好了!
將struts.xml修改下下
<?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="default" extends="struts-default">
        <action name="login" class = "com.hello.model.UserLoginAction">
            <result>
                mainMenuContent.jsp
            </result>
        </action>
    </package>

</struts>
 
index.html也修改下
<!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>
	<a href="login?userName=zoe?password=56"">User Zoe Login!</a>
</body>
</html> 
 mainMenuContent.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	Add operation successes!
</body>
</html>
 
運行後,發現輸出setUserName。看來我們的輸入沒有白費,雖然此時的輸入是我們之前就利用請求參數設置好了,但是也是小小的進步,之後可以用html的input~~
慢慢來~
在小小的修改下struts.xml
<?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="default" extends="struts-default">
        <action name="login" class = "com.hello.model.UserLoginAction" method="add" >
            <result>
                mainMenuContent.jsp
            </result>
        </action>
    </package>

</struts>
 
輸出:
setUserName
zoe?password=56 has been accepted!
目前對MVC有小小的感覺了!
Struts作爲MVC中C(Controler),主要就是根據用戶的輸入,選擇相應的操作並根據操作的結果輸出。
Struts就是配置,就是個工具。
困了~睡覺了!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章