当前位置: 代码迷 >> Web前端 >> Struts2视图中转类型
  详细解决方案

Struts2视图中转类型

热度:397   发布时间:2012-11-23 22:54:33.0
Struts2视图转发类型
?在Struts2中result的视图转发类型比较常用的有四种:dispathcher(默认值):服务器内部请求转发类型;? redirect:重定向到某个jsp文件;? redirectAction:重定向到某个action;? plainText:他主要用在输出页面源代码。
在struts.xml中配置如下:
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE struts PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4 "http://struts.apache.org/dtds/struts-2.0.dtd">
5
6 <struts>
7 <package name="lz" namespace="/control/employee" extends="struts-default">
8 <!-- 浏览器重定向到修改界面 -->
9 <action name="redirect" class="com.lz.action.UserName" method="execute">
10 <result name="success" type="redirect">/redirect.jsp?username=${Savepath}</result>
?
在web.xml中:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app version="2.4"
3 xmlns="http://java.sun.com/xml/ns/j2ee"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
6 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
7
8 <filter>
9 <filter-name>struts2</filter-name>
10 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
11 </filter>
12 <filter-mapping>
13 <filter-name>struts2</filter-name>
14 <url-pattern>/*</url-pattern>
15 </filter-mapping>
16
17 <welcome-file-list>
18 <welcome-file>index.jsp</welcome-file>
19 </welcome-file-list>
20 </web-app>
?
再定义一个UserName.java
1 package com.lz.action;
2
3 import java.net.URLEncoder;
4
5 public class UserName {
6 private String Savepath;
7
8 public void setSavepath(String savepath) {
9 Savepath = savepath;
10 }
11
12 public String getSavepath() {
13 return Savepath;
14 }
15
16 public String execute()throws Exception
17 {
18 Savepath=URLEncoder.encode("天下第一","utf-8");
19
20 return "success";
21 }
22
23 }
?页面代码:
<%@ 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的源码了。并且可以支持中文格式的了。
?
?
  相关解决方案