`

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics