creating .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?bl开发者_如何学Cablabla... 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.
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
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).
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.
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.
精彩评论