您的位置:首页 > 编程语言 > Java开发

Struts2中result配置的各路视图转发类型

2011-03-30 22:20 357 查看
在Struts2中result的视图转发类型比较常用的有四种:dispathcher(默认值):服务器内部请求转发类型; redirect:重定向到某个jsp文件; redirectAction:重定向到某个action; plainText:他主要用在输出页面源代码。
在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="lz" namespace="/control/employee" extends="struts-default">
<!-- 浏览器重定向到修改界面 -->
<action name="redirect" class="com.lz.action.UserName" method="execute">
<result name="success" type="redirect">/redirect.jsp?username=${Savepath}</result>

在web.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

再定义一个UserName.java

package com.lz.action;

import java.net.URLEncoder;

public class UserName {
private String Savepath;

public void setSavepath(String savepath) {
Savepath = savepath;
}

public String getSavepath() {
return Savepath;
}

public String execute()throws Exception
{
Savepath=URLEncoder.encode("天下第一","utf-8");

return "success";
}

}

页面代码:

<%@ page language="java" import="java.util.*,java.net.URLDecoder" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>redrect</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">

</head>

<body>
<%= URLDecoder.decode(new String(request.getParameter("username").getBytes("ISO8859-1"),"UTF-8"),"UTF-8") %><br/>
<%= URLDecoder.decode(new String(request.getParameter("username").getBytes("ISO8859-1"),"UTF-8"),"UTF-8")%>

</body>
</html>

这样,运行。页面上就会出现“天下第一”四个字。
接下来是redirectAction类型。在struts.xml增加action标签。

<?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="lz" namespace="/control/employee" extends="struts-default">
  <action name="redirectAction" class="com.lz.action.UserName" method="execute">
<result name="success" type="redirectAction">/hello</result>
</action>
</package>
<package name="other" namespace="/control/department" extends="struts-default">
<action name="redirect">
<result>/index.jsp</result>
</action>
</package>
</struts>

这样在浏览器中输入:http://localhost:8080/control/department/redirect.action就会跳到hello.jsp页面里。
还有最后一种:plainText。他主要用在输出页面源代码。
如下所示:修改struts.xml文件。

<action name="plainText">
<result type="plainText">
<param name="location">/redirect.jsp</param>
<param name="charSet">UTF-8</param><!-- 制定读取文件的编码 -->
</result>
</action>

<%@ page language="java" import="java.util.*,java.net.URLDecoder" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>redrect</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">

</head>

<body>
你好!

</body>
</html>

这样,就可以输出redirect.jsp的源码了。并且可以支持中文格式的了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: