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

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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值