1

我正在关注的Struts 2 Hello World Annotation Example教程由Mkyong:@Result在类级别和方法级别

@Namespace("/User") 
@ResultPath(value="/") 
@Action(value="/welcome", 
     results={@Result(name="success", location="pages/welcome_user.jsp")}) 
public class WelcomeUserAction extends ActionSupport { 

    public String execute(){ 
     return SUCCESS; 
    } 
} 

访问http://localhost:8080/project_name/User/welcome工作正常的URL。

现在,我试图从一流水平的@Action(因此@Result)注释移动到方法的层次:

@Namespace("/User") 
@ResultPath(value="/") 
public class WelcomeUserAction extends ActionSupport { 

    @Action(value="/welcome", 
      results={@Result(name="success", location="pages/welcome_user.jsp")})  
    public String execute(){ 
     return SUCCESS; 
    } 
} 

但这样做后,我得到的404错误:

/project_name/pages/welcome_user.jsp is not found.

我的JSP是下

/WebContent/User/pages 

这究竟是为什么?

+0

在你的配置中为'struts.enable.SlashesInActionNames'设置了什么?只需删除操作名称中的斜杠即可 - >'@Action(value =“welcome”'。 –

+0

用这个解决方案回答这个问题@AleksandrM –

回答

1

由于Struts2的会找你的JSP中

WebContent/@ResultPath/@Namespace/@Result 

而不是做

@ResultPath("/")/@Namespace("/User")/@Result("pages/welcome_user.jsp") 

你可以从

WebContent/User/pages/welcome_user.jsp 

移动你的JSP来

WebContent/pages/User/welcome_user.jsp 

,然后使用

@ResultPath("/pages")/@Namespace("/User")/@Result("welcome_user.jsp") 

此时,下面的两个配置应该工作:

随着@Action类级别

@ResultPath(value="/pages") 
@Namespace("/User") 
@Action(value="/welcome", results={@Result(name="success", location="welcome_user.jsp")}) 
public class WelcomeUserAction extends ActionSupport { 

    public String execute(){ 
     return SUCCESS; 
    } 
} 

随着@Action方法级

@ResultPath(value="/pages") 
@Namespace("/User") 
public class WelcomeUserAction extends ActionSupport { 

    @Action(value="/welcome", results={@Result(name="success", location="welcome_user.jsp")}) 
    public String execute(){ 
     return SUCCESS; 
    } 
} 

我不知道为什么Mkyong的例子只适用于课堂级别的注释,而我正在等待更多专家来充实我们的好奇心;同时,这应该是你需要的。

+1

谢谢Andrea Ligios! – Jake

相关问题