如何使用 Stripes 框架删除文件后缀/扩展名(.jsp 和 .action)?

发布于 2024-08-21 05:17:27 字数 1227 浏览 10 评论 0原文

我希望在我的网络应用程序中使用漂亮/干净的 URL。

我想要以下 URL:

http://mydomain.com/myapp/calculator

.. 解析为:

com.mydomain.myapp.action.CalculatorActionBean

我尝试用以下内容覆盖 NameBasedActionResolver:

public class CustomActionResolver extends NameBasedActionResolver {
    public static final String DEFAULT_BINDING_SUFFIX = ".";

    @Override
    protected String getBindingSuffix() {
        return DEFAULT_BINDING_SUFFIX;
    }

    @Override
    protected List<String> getActionBeanSuffixes() {
        List<String> suffixes = new ArrayList<String>(super.getActionBeanSuffixes());
        suffixes.add(DEFAULT_BINDING_SUFFIX);
        return suffixes;
    }
}

并将其添加到 web.xml

<servlet-mapping>
    <servlet-name>StripesDispatcher</servlet-name>
    <url-pattern>*.</url-pattern>
</servlet-mapping>

这让我得到:

http://mydomain.com/myapp/Calculator.

但是:

  1. 一个流浪的“.”仍然既不漂亮也不干净。
  2. URL 中的类名仍然大写..?
  3. 那仍然让我留下 *.jsp..?是否有可能同时摆脱 .action.jsp

I'm looking to use pretty / clean URL's in my web app.

I would like the following URL:

http://mydomain.com/myapp/calculator

.. to resolve to:

com.mydomain.myapp.action.CalculatorActionBean

I tried overwriting the NameBasedActionResolver with:

public class CustomActionResolver extends NameBasedActionResolver {
    public static final String DEFAULT_BINDING_SUFFIX = ".";

    @Override
    protected String getBindingSuffix() {
        return DEFAULT_BINDING_SUFFIX;
    }

    @Override
    protected List<String> getActionBeanSuffixes() {
        List<String> suffixes = new ArrayList<String>(super.getActionBeanSuffixes());
        suffixes.add(DEFAULT_BINDING_SUFFIX);
        return suffixes;
    }
}

And adding this to web.xml:

<servlet-mapping>
    <servlet-name>StripesDispatcher</servlet-name>
    <url-pattern>*.</url-pattern>
</servlet-mapping>

Which gets me to:

http://mydomain.com/myapp/Calculator.

But:

  1. A stray "." is still neither pretty nor clean.
  2. The class name is still capitalized in the URL..?
  3. That still leaves me with *.jsp..? Is it even possible to get rid of both .action and .jsp?

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

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

发布评论

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

评论(3

眉目亦如画i 2024-08-28 05:17:27

我认为您正在寻找 @URLBinding 注释。查看 @URLBinding豆。

@UrlBinding("/计算器")

I think you are looking for the @URLBinding annotation. Look at @URLBinding on your Bean.

@UrlBinding("/calculator")

┊风居住的梦幻卍 2024-08-28 05:17:27

我试图做同样的事情,并且有同样的问题,尽管我希望我的 URL 使用尾部斜杠 http://mydomain.com/myapp/calculator/

答案是使用 @ UrlBinding & DynamicMappingFilter

我将示例修改为:

@UrlBinding("/calculator/")
public class CalculatorActionBean implements ActionBean {
  .
  .
  .
return new ForwardResolution("/WEB-INF/view/calculator.jsp");

然后我将 DMF 添加到 web.xml:

<filter>
    <display-name>Stripes Dynamic Mapping Filter</display-name>
    <filter-name>DynamicMappingFilter</filter-name>
    <filter-class>net.sourceforge.stripes.controller.DynamicMappingFilter</filter-class>
    <init-param>
        <param-name>ActionResolver.Packages</param-name>
        <param-value>com.example.stripes</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>DynamicMappingFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
</filter-mapping>

现在干净的 URL 按预期工作,并且在与表单交互后我永远不会重定向到 *.action URL。

I was trying to do the same thing, and had the same question, though I wanted my URL to use the trailing slash http://mydomain.com/myapp/calculator/

The answer is to use @UrlBinding & the DynamicMappingFilter

I modified the example to have:

@UrlBinding("/calculator/")
public class CalculatorActionBean implements ActionBean {
  .
  .
  .
return new ForwardResolution("/WEB-INF/view/calculator.jsp");

Then I added the DMF to web.xml:

<filter>
    <display-name>Stripes Dynamic Mapping Filter</display-name>
    <filter-name>DynamicMappingFilter</filter-name>
    <filter-class>net.sourceforge.stripes.controller.DynamicMappingFilter</filter-class>
    <init-param>
        <param-name>ActionResolver.Packages</param-name>
        <param-value>com.example.stripes</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>DynamicMappingFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
</filter-mapping>

Now the clean URL works as expected, and I'm never redirected to a *.action URL after interacting with the form.

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