redirect跳转不能访问web-inf下的jsp

来源:互联网 发布:展示网站cms 编辑:程序博客网 时间:2024/05/14 17:13

1、<result type="redirect">/***.jsp</result>

 

redirect的路径一定不能在WEB-INF路径下,因为redirect是相当于用户直接访问了路径,而用户不能访问WEB-INF目录下的文件,只有程序内部转发的时候才能转发到WEB-INF下的JSP。

 

<result type="redirect">/***.jsp?username=${username}</result>

 

url传递中文时,action需要做处理:

 

public String execute () throws UnsupportedEncodingException {
  this.username = URLEncoder.encode("传智播客", "UTF-8");
  msg = "我的第一个Struts2应用";
  return "success";

}

 

jsp中可直接:${param.username}

或Java脚本:URLDecoder.decode(new String(request.getParameter("username").getBytes("ISO8859-1"), "UTF-8"), "UTF-8")

 

2、redirectAction
下面是redirectAction结果类型的例子,如果重定向的action在同一个包下:
<result type="redirectAction">helloworld</result>
helloword 为定义的<action/>的name。

如果重定向的action在别的命名空间下:
<result type="redirectAction">
 <param name="actionName">helloworld</param>
 <param name="namespace">/test</param>
</result>

 

3、 plainText
plainText:显示原始文件内容,例如:当我们需要原样显示jsp文件源代码的时候,我们可以使用此类型。
<result name="source" type="plainText">
 <param name="location">/xxx.jsp</param>
 <param name="charSet">UTF-8</param><!-- 指定读取文件的编码 -->
</result>

 

4、可以供很多页面共用的视图
struts.xml中增加:
     <global-results>
      <result name="message">/WEB-INF/page/message.jsp</result>
     </global-results>

        <action name="manage" class="cn.itcast.action.HelloWorldAction" method="add">
        </action>

message.jsp:
<body>
 结果
</body>

HelloWorldAction中增加:
 public String add() {
  return "message";
 }

访问:http://localhost:8080/struts2/test/manage
显示message.jsp

0 0
原创粉丝点击