我想要以下格式的 URL
http://localhost/users/abc
而不是
http://localhost/users?name=abc
如何在 Struts2 中实现这一点?
I want the URL of the below format
rather than
How to achieve this in Struts2?
您可以使用 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.
您可以使用http://tuckey.org/urlrewrite/。
You can use http://tuckey.org/urlrewrite/.
我们可以使用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
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
If you observe above mapping, "struts.enable.SlashesInActionNames" should be set "true" in order to achieve this.
Here the action support class
After executing this program, you'll get "Santhosh" value populated into userName property.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
暂无简介
文章 0 评论 0
接受
发布评论
评论(3)
您可以使用 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.
您可以使用http://tuckey.org/urlrewrite/。
You can use http://tuckey.org/urlrewrite/.
我们可以使用struts2来实现这一点。您需要做的就是在操作名称中使用通配符。让我用你的案例解释一下
这就是你想要的最终 URL。你会想要捕获该值,即; “Santhosh”在行动支持。
我假设您已准备好在末尾添加 struts2 后缀 ie; “.action”或“.html”(我已将操作类扩展名自定义为.html)。
只需在 struts.xml 文件中定义如下映射
如果您观察上面的映射,“struts.enable.SlashesInActionNames”应设置为“true”才能实现此目的。
这里是操作支持类
执行此程序后,您将获得填充到 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
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
If you observe above mapping, "struts.enable.SlashesInActionNames" should be set "true" in order to achieve this.
Here the action support class
After executing this program, you'll get "Santhosh" value populated into userName property.