Struts2自定义Action,自定义方法,DMI(动态方法调用),路径问题

来源:互联网 发布:淘宝怎么买百度云账号 编辑:程序博客网 时间:2024/06/05 19:52

注意配置文件中的result JSP 必须加/(代表当前应用)

action类的编写:可以什么都不继承与实现,可以实现Action接口,可以继承ActionSupport,但推荐第三种

package struts.zyl;import com.opensymphony.xwork2.ActionSupport;public class IndexAction extends ActionSupport {public String add(){// TODO Auto-generated method stubreturn "success";}}

可以使用

http://localhost/struts2_action/path/userAdd

http://localhost/struts2_action/path/user!add(DMI动态方法调用,默认是关闭的,需要添加一句代码)

<constant name="struts.enable.DynamicMethodInvocation" value="true" />

访问add.jsp


struts路径问题:

当在客户端输入http://localhost/struts2_action/path/index的时候,路径是根据struts2中的action路径而不是jsp的本地路径.

当用户想要从这个index.action中跳转回/index.jsp的时候..

通常会这样写

<a href="index.jsp">点我</a>

虽然就算这个jsp文件和index.jsp同一个目录,这样只会访问到本路径下的http://localhost/struts2_action/path/index.jsp

不能使用相对路径,

那么我们这样写呢?

<a href="/index.jsp">点我</a>

这样更不行,这样就返回到了http://localhost/index.jsp去了,连webapp都没有进入

我们只好这样写

<a href="/struts_action/index.jsp">点我</a>

这样倒是可以了,但是每次都要加上自己的项目名称未免太过麻烦

还好myeclipse帮我们自动解决了这个问题

<%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
我们可以这样写
<a href="<%=basePath%>index.jsp">点我</a>

但是有更方便的:

 <base href="<%=basePath%>">

这样我们每次跳转到index.jsp都是绝对路径了。

0 0
原创粉丝点击