带 .action 的 Struts2 URL

发布于 2024-08-13 19:44:37 字数 275 浏览 4 评论 0 原文

我想要以下格式的 URL

http://localhost/users/abc

而不是

http://localhost/users?name=abc

如何在 Struts2 中实现这一点?

I want the URL of the below format

http://localhost/users/abc

rather than

http://localhost/users?name=abc

How to achieve this in Struts2?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

停顿的约定 2024-08-20 19:44:37

您可以使用 REST 插件约定插件(请参阅此博文 了解有关后续解决方案的更多详细信息)。其他选项包括 servlet 过滤或 mod_rewrite。

You could use the REST Plugin or the convention plugin (see this blog post for more details on the later solution). Other options include servlet filtering or mod_rewrite.

天荒地未老 2024-08-20 19:44:37

我们可以使用struts2来实现这一点。您需要做的就是在操作名称中使用通配符。让我用你的案例解释一下

http://localhost/users/Santhosh

这就是你想要的最终 URL。你会想要捕获该值,即; “Santhosh”在行动支持。

我假设您已准备好在末尾添加 struts2 后缀 ie; “.action”或“.html”(我已将操作类扩展名自定义为.html)。

只需在 struts.xml 文件中定义如下映射

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
  <constant name="struts.devMode" value="false" />
  <constant name="struts.action.extension" value="html" />
  <constant name="struts.enable.SlashesInActionNames" value="true"/>

  <action name="users/*" class="com.srm.action.UserAction" method="execute">
    <param name="userName">{1}</param>
    <result name="success" type="tiles">addEditCategory</result>
  </action>
</struts>

如果您观察上面的映射,“struts.enable.SlashesInActionNames”应设置为“true”才能实现此目的。

这里是操作支持类

public class UserAction extends ActionSupport
{
  private static final long serialVersionUID = 1L;
  private String userName = null;

  public String execute() throws Exception
  {
    System.out.println("Username: "+userName);
    return SUCCESS;
  }

  public void setUserName(String userName)
  {
    this.userName = userName;
  }

  public String getUserName()
  {
    return userName;
  }
}

执行此程序后,您将获得填充到 userName 属性中的“Santhosh”值。

We can achieve this using struts2. All you need to do is, use wildcard characters in your action name. Let me explain with your case

http://localhost/users/Santhosh

This is what you wanted as the end URL. And you would want to capture the value i.e.; "Santhosh" in action support.

I'm assuming that you're ready to add a struts2 suffix at the end i.e.; ".action" or ".html" (I've customized my action classes extension as .html).

Just define the mapping as below in struts.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
  <constant name="struts.devMode" value="false" />
  <constant name="struts.action.extension" value="html" />
  <constant name="struts.enable.SlashesInActionNames" value="true"/>

  <action name="users/*" class="com.srm.action.UserAction" method="execute">
    <param name="userName">{1}</param>
    <result name="success" type="tiles">addEditCategory</result>
  </action>
</struts>

If you observe above mapping, "struts.enable.SlashesInActionNames" should be set "true" in order to achieve this.

Here the action support class

public class UserAction extends ActionSupport
{
  private static final long serialVersionUID = 1L;
  private String userName = null;

  public String execute() throws Exception
  {
    System.out.println("Username: "+userName);
    return SUCCESS;
  }

  public void setUserName(String userName)
  {
    this.userName = userName;
  }

  public String getUserName()
  {
    return userName;
  }
}

After executing this program, you'll get "Santhosh" value populated into userName property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文