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的源码了。并且可以支持中文格式的了。
在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的源码了。并且可以支持中文格式的了。
相关文章推荐
- Struts2中result配置的各路视图转发类型
- struts2之Action配置的各项默认值、result配置的各种视图转发类型及为应用指定多个struts配置文件
- struts2学习------result配置的各种视图转发类型
- struts2 --result配置的各种视图转发类型
- Struts2中result配置中常见的几种视图转发类型
- struts2x学习笔记6_result配置的各种视图转发类型
- result配置的各种视图转发类型3
- result配置各种视图转发类型
- struts2之Action配置的各项默认值、result配置的各种试图转发类型及为应用指定多个struts配置文件
- Struts2学习笔记之result视图转发类型
- struts2之Action配置的各项默认值、result配置的各种试图转发类型及为应用指定多个struts配置文件
- struts2中Action配置中的各项默认值和result的各种转发类型
- Struts2笔记02 Struts2中result配置中各种视图转发类型
- Struts2学习笔记之result视图转发类型
- struts2_3_配置各种视图转发类型
- Struts2学习笔记(三):result配置的各项视图转发类型
- Struts2中Action配置的默认值和result配置的转发类型
- Struts2学习笔记(四):result配置的各种视图转发类型
- struts2之Action配置的各项默认值、result配置的各种试图转发类型及为应用指定多个struts配置文件
- result配置的各种视图转发类型