struts2day06 Struts2.0的result

Struts2.0的result

       Action處理請求後,會返回一個字符串,這整個字符串就是一個邏輯視圖。Strtus2.0會根據邏輯視圖和物理視圖的映射關係,找到物理視圖。
   <result name="xxx" type="xxxx"></result>

  1.  dispatcher
  <result name="xxx" type="dispatcher">
       <!--location表示實際視圖資源-->
       <param name="location">/ok.jsp</param>
       <!--parse:在視圖頁面中是否可以使用ognl表達式,默認爲true-->
       <param name="parse"></param>
  </result>
  
    2.stream Action給客戶端的是一個輸入流
    <result name="xxx" type="stream">
        <param name="inputName">action中流的屬性名</param>
    <param name="buffSize">緩衝【默認值爲1024】</param>
    </result>
  
   3.chain 一個Action轉發到另一個Action
   4. redirect 頁面的重定向
   5. redirectAction  一個Action重定向到另一個Action
   6. freemarker:使用指定的FreeMarker模板作爲視圖的類型
        模板:一份已經寫好了基本內容,固定格式的文檔,會空出或者使用佔位符的內容。
          用戶使用時只要填充空出的位置即可。
   7. plainText:顯示頁面源代碼

 

FreeMaker例子:

1.寫一個Action extends ActionSupport

package com.jsu.struts2.action;

import com.opensymphony.xwork2.ActionSupport;

public class FreeAction extends ActionSupport {
	private String title;
	private String body;
	public String execute() throws Exception {
		title="FreeMaker Test";
		body="Hello FreeMarker";
		return SUCCESS;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getBody() {
		return body;
	}
	public void setBody(String body) {
		this.body = body;
	}
	
}

 2.在webroot下面創建freeTest.ftl文件

<html>
 <head>
  <title>${title}</title>
 </head>
 <body>
 <h1>${body}</h1>
 </body>
</html>

 3.在struts.xml文件中配置

<?xml version="1.0" encoding="UTF-8" ?>
  <!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
	"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
	<package name="resultDemo" namespace="/" extends="struts-default">	
		<action name="free" class="com.jsu.struts2.action.FreeAction">
		<result type="freemarker">/freeTest.ftl</result>
		</action>
	</package>
</struts>

 4.在瀏覽器地址欄訪問http://localhost:8080/Struts2_06/free.action

 

PlainText例子

1.寫一個Action PlaintTextAction

package com.jsu.struts2.action;

import com.opensymphony.xwork2.ActionSupport;

public class PlainTextAction extends ActionSupport{
	private String hello;
	public String execute() throws Exception {
		hello="你好  Plain Text";
		return SUCCESS;
	}
	public String getHello() {
		return hello;
	}
	public void setHello(String hello) {
		this.hello = hello;
	}
	
}

 2.在ok.jsp頁面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>  
    <title>Plint Text</title>
  </head> 
  <body>
   <h1>${hello} </h1>
  </body>
</html>

 3.在struts.xml文件中配置

<?xml version="1.0" encoding="UTF-8" ?>
  <!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
	"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
	<package name="resultDemo" namespace="/" extends="struts-default">		
		<action name="plain" class="com.jsu.struts2.action.PlainTextAction">
			<result type="plainText">
				<param name="location">/ok.jsp</param>
				<param name="charSet">UTF-8</param>
			</result>
		</action>
	</package>
</struts>

 4.在地址欄訪問http://localhost:8080/Struts2_06/plain.action

發佈了64 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章