Struts 2 插件转换——从代码隐藏到约定插件 [英] Struts 2 plugin Conversion - from Codebehind to Convention plugin

查看:25
本文介绍了Struts 2 插件转换——从代码隐藏到约定插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对将 codebehind 注释转换为传统的 struts.xml 文件感到困惑.

I'm getting confusion to converting the codebehind annotation to conventional struts.xml file.

如何在动作类中识别动作名称?因为如果 write method public String list{} - action 它与 JSPs 的匹配 product-list.jsp 将自动识别页面和 URL 为 product!list 像那样.什么是传统插件?

How to identify action name in action class? Because if write method public String list{} - action its match with JSPs product-list.jsp will automatically identify the page and URL was product!list like that. What is going to be conventional plugin ?

当前网址:

http://localhost:7001/example/product!search- jsp 名称 product-search.jspProductAction - 动作类.

http://localhost:7001/example/product!search- jsp name product-search.jsp and ProductAction - action class.

请告诉我如何配置 struts.xml 文件,它相当于上面的配置.

Please tell me how to configure the struts.xml file which equivalent above config.

我尝试过如下:

<package name="example" namespace="/" extends="struts-default">
  <action name="Search">
    <result>product-search.jsp</result>
  </action>         
</package>

错误:

org.apache.struts2.dispatcher.Dispatcher - Could not find action or result
There is no Action mapped for namespace / and action name part. - [unknown location]

推荐答案

在 Struts2 中,动作被映射到方法上.上面的url你应该映射为

In Struts2 actions are mapped on methods. The above url you should map as

<package name="example" namespace="/" extends="struts-default">
   <action name="product" method="search"> <!-- case sensitive -->
     <result>product-search.jsp</result>
   </action>
</package>

或通过注释

@Namespace("/")
public class ProductAction extends ActionSupport {

  public String execute() {
    return SUCCESS;

  }

  @Action(value="product",
    results=@Result(location="/product-list.jsp")
  )
  public String search() {
    return SUCCESS;
  }
}

注意,execute 方法没有被映射,所以它不会执行.如果您需要执行该方法,您应该创建到它的映射.为此,您可以在类或方法 execute 上放置注释.

Notice, that the method execute is not mapped, so it will not execute. If you need that method execute you should create mapping to it. For this purpose you could place annotation on class or on method execute.

您可以在 Convention Plugin 页面上找到更多示例.

More examples you could find on the page Convention Plugin.

这篇关于Struts 2 插件转换——从代码隐藏到约定插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆