Html 如何使用 <a href> 标签调用动作类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5429874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:34:25  来源:igfitidea点击:

How to call action class using <a href> tag

htmljspstruts2

提问by pankaj

The following code is giving error as:

以下代码给出的错误为:

There is no Action mapped for namespace / and action name .

没有为命名空间 / 和动作名称映射的动作。

code: .jsp file:

代码:.jsp 文件:

<a href="<s:url action='DisconnectAction' />">Disconnect</a> 

struts.xml

struts.xml

<action name="DisconnectAction" class="ActionPackages.DisconnectAction">
    <result name="success">/JSP/mytemplate.jsp</result>
    <result name="input">/JSP/mytemplate.jsp</result>
    <result name="error">/JSP/mytemplate.jsp</result>
    <result name="failure">/JSP/mytemplate.jsp</result>
</action>

I want to perform action and keep the current accessing page only which is mytemplate.jsp

我想执行操作并仅保留当前访问页面 mytemplate.jsp

回答by Umesh Awasthi

Though your question is not clear so better give details about the problem but if i read the subject you want to call action using so here is the struts2 standard way

虽然你的问题不清楚,所以最好提供有关问题的详细信息,但如果我阅读了你想使用的主题,那么这里是 struts2 标准方式

<s:a cssClass="table_tabs_dis" href="%{GenURL}">General</s:a>

and here is one more

还有一个

<a href="<s:url action=MyAction" namespace="/abc/xyz" />">ClickME</a>

回答by Raki

According to your question the problem was in your <s:url/>tag you have to give actionafter DisconnectAction & it will work fine as you want.

根据您的问题,问题出在您的<s:url/>标签中,您必须action在 DisconnectAction 之后给出,它会按您的意愿正常工作。

<a href="<s:url action="DisconnectAction.action"/>">Disconnect</a>

Struts.xml

Struts.xml

<action name="DisconnectAction" class="ActionPackages.DisconnectAction">
    <result name="success">/JSP/mytemplate.jsp</result>
    <result name="input">/JSP/mytemplate.jsp</result>
    <result name="error">/JSP/mytemplate.jsp</result>
    <result name="failure">/JSP/mytemplate.jsp</result>
</action>

回答by Aditya Ekbote

It seems that you have not given ".action "extension in struts tag. When you are using

似乎您没有在 struts 标签中给出“.action”扩展名。当您使用

<s:form action="someAction"></s:form>

you dont need to give .action extension to your action name. But since you are embedding it in HTML tag, you have to give .action Extension.

您不需要为您的操作名称提供 .action 扩展名。但是由于您将其嵌入到 HTML 标记中,因此您必须提供 .action 扩展名。

(Answer Verified on Live)

(现场验证答案)

 <a href="<s:url action="urlTagAction.action" />"> Disconnect </a>