创建.htaccess

发布于 2024-10-07 16:32:40 字数 319 浏览 0 评论 0原文

我正在使用Struts2开发一个项目。我的网页端使用jsp。我的应用程序使用 MySQL 数据库,并为其安装了 wamp 服务器。我的网址就像 localhost/xxx?name.action?blablabla... 而不是这个,我想重定向网址。例如,我想使用 localhost/name 或类似的东西,而不是我的网址上的 name.action 。另外,对于 .htaccess,我想通过它发送数据,例如 name.action?Id=$1... 以用于我在 Struts2 端的操作。但是我无法重定向网址,无法正确定义文档根目录和 .htaccess 文件。我该怎么办,能一步步解释一下吗?

谢谢。

I am developing a project with Struts2. I use jsp for web-side. I use MySQL database for my application and I installed wamp server for it. My urls are like localhost/xxx?name.action?blablabla... instead of this I wanna redirect urls. For example instead of name.action on my url I wanna use localhost/name or something like that. Also with .htaccess I wanna send data over it as like name.action?Id=$1... for my action at Struts2 side. However I couldn't redirect the urls, I couldn't define document root properly and .htaccess file. What should I do, can you explain it step by step?

Thanks.

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

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

发布评论

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

评论(4

起风了 2024-10-14 16:32:40

您的配置将取决于您要使用的 Java EE 服务器品牌。 Apache Tomcat 与 Apache Web 服务器有非常好的 AJP 集成,因此我将在本示例配置中使用它。

您写道您想在 Apache 中进行 URL 重新映射。我强烈建议不要这样做,因为这将使在应用程序中动态生成 URL 变得非常困难。您可以更好地使用 Web 框架的 URL 映射功能,以保持该框架的链接生成支持。

我对 Struts 不熟悉,但如果它不支持这个基本功能,我建议您切换到 Stripes 框架或更复杂的Spring MVC框架。你进一步写你想使用JSP,所以我也建议你使用 MVC模式所有这些框架都支持。这意味着您将 URL 映射到操作/控制器对象,而不是直接映射到 JSP 文件。

下面是一个 .htaccess 配置示例,它将把所有以 .action 结尾的传入 URL 传递到您的 Java EE 服务器。 URL 的传递方式与请求的 URL 完全相同,包括它们的(可选)参数。确保端口 8009 是 Java EE 服务器的 AJP 侦听器端口(或者将其更改为您已配置的端口)。

# Example .htaccess
#
# Please make sure 8009 is the port number of your Java EE server
RewriteEngine On
RewriteBase /

RewriteRule ^(.*\.action)$ ajp://127.0.0.1:8009/$1 [P,QSA,L]

还要确保您已正确配置 Apache。例如,您需要配置正确的模块,这些是我已启用的模块(尽管您可能不需要全部):

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule rewrite_module modules/mod_rewrite.so

Your configuration will depend on what brand Java EE server you’re going to use. Apache Tomcat has a very good AJP integration with Apache webserver so that’s what I will be using in this example configuration.

You write that you would like to do URL remapping in Apache. I strongly advice against it, as this will make it very hard to dynamically generate you’re URL in your application. You can better use the URL mapping features of your web framework in order to keep link generating support of that framework.

I’m not familiar with Struts, but if it does not support this basic feature I would recommend you switch to the Stripes framework or the more complex Spring MVC framework. You further write you want to use JSP, so I also recommend you use the MVC pattern all these framework support. That means you map URL's to action/controller objects and never directly to JSP files.

Below is an example .htaccess configuration that will pass all incoming URL's that end on .action to your Java EE server. The URL's are passed through exactly the same as the requested URL including their (optional) parameters. Make sure that port 8009 is the AJP listener port of your Java EE server (or else change it to the port you have configured).

# Example .htaccess
#
# Please make sure 8009 is the port number of your Java EE server
RewriteEngine On
RewriteBase /

RewriteRule ^(.*\.action)$ ajp://127.0.0.1:8009/$1 [P,QSA,L]

Also make sure you have configured Apache correctly. For example you need to have configured the correct modules, these are the ones I have enabled (although you might not need all them):

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule rewrite_module modules/mod_rewrite.so
神回复 2024-10-14 16:32:40

您不需要在 Struts2 中使用 .action 扩展名。您可以选择不同的扩展名或不选择扩展名(听起来这就是您想要的)。

You don't need to use the .action extension in Struts2. You can opt for a different extension or no extension (which it sounds like is what you want).

壹場煙雨 2024-10-14 16:32:40

如果使用当前版本的 struts2, 2.2.1,您应该按照以下方式设置您的应用程序: http://struts.apache.org/2.2.1/docs/create-struts-2 -web-application-using-maven-to-manage-artifacts-and-to-build-the-application.html 完成此操作后,请在页面中搜索“web.xml Servlet Filter”“名称”并“name.action”都是到“name”操作的有效映射。至于 url 参数,这是由 struts 表单标签自动处理的。 struts.apache.org 网站上的基本教程和标记参考示例。文档根通常是通过 IDE 从项目名称设置的,此外,当您编译时,IDE 通常会创建一个 WAR,当通过应用程序服务器/Web 容器的 Web 界面部署 WAR 时,更改文档根通常是可见字段之一在安装过程中。因此,可以根据每个安装轻松配置。

If using a current version of struts2, 2.2.1 you should set up your application as per: http://struts.apache.org/2.2.1/docs/create-struts-2-web-application-using-maven-to-manage-artifacts-and-to-build-the-application.html please search the page for "web.xml Servlet Filter" once this is done "name" and "name.action" are both valid mappings to the "name" action. As for url parameters this is automatically taken care of with struts form tags. The basic tutorials and tag reference examples on the struts.apache.org web site. Document root is normally set up via IDE from project name, further when you compile typically the IDE will create a WAR, when deploying the WAR via a web interface for your application server / web container changing the document root is normally one of the visible fields during installation. So that is easily configurable on a per installation basis.

七婞 2024-10-14 16:32:40

Steven 说得对,

当你从 jsp 调用 struts 动作时,只需使用动作名称即可。

例如,锚标记中的 href="actionName" 而不是 href="actionName.action"

Steven is saying right

Just use action name when you call struts action from jsp.

e.g. href="actionName" instead of href="actionName.action" in a anchor tag.

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