让 Spring DispacherServlet 解析正确的 URLmap
在我的 Tomcat-Spring-Java 项目中,我想要以下 URLS
/index - 显示入口页面
/login - 登录页面
/cars/ - 列出可用的汽车
/cars/{id} - 显示特定汽车
/cars/{id}/action - 对这辆特定的汽车执行操作
/people/ - 列出可用的人员
/people/{id} - 显示特定的人
/people/{id}/action - 对这个特定的人执行操作
我在 web.xml 中得到了这样的调度程序 servlet 映射。
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/login</url-pattern>
<url-pattern>/logout</url-pattern>
<url-pattern>/index</url-pattern>
<url-pattern>/cars/*</url-pattern>
<url-pattern>/people/*</url-pattern>
</servlet-mapping>
我的 dispatcher-servlet.xml 映射如下:
<bean id="urlMap"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<props>
<prop key="/login">loginController</prop>
<prop key="/logout">logoutController</prop>
<prop key="/index">welcomeController</prop>
<prop key="/cars">listCarsController</prop>
<prop key="/cars/">listCarsController</prop>
<prop key="/cars/*">showCarController</prop>
<prop key="/cars/*/action">actionCarController</prop>
<prop key="/people">listPeopleController</prop>
<prop key="/people/">listPeopleController</prop>
<prop key="/people/*">showPersonController</prop>
<prop key="/people/*/action">actionPersonController</prop>
</props>
</property>
</bean>
这没有按预期工作,因为当我访问例如 /people/1/action 时,Dispacher servlet servlet 说正在寻找 [1/action] 的处理程序当然,它不是像搜索一样,而是在前面加上people/来将人员id和汽车id分开。
就像“*”将是输入的唯一 URL。
顺便说一句,我想减少 URL 的扩展名,我知道将调度程序映射到 web.xml 文件上的 *.htm 就可以解决这个问题。
但它必须是一种映射方法调度程序或让调度程序搜索完整的 url 定义。
On my Tomcat-Spring-Java project I want to have the following URLS
/index - to display entry page
/login - Login page
/cars/ - list the available cars
/cars/{id} - show a particular car
/cars/{id}/action - do action on this particular car
/people/ - list the available people
/people/{id} - show a particular person
/people/{id}/action - do action on this particular person
Ive got my dispacher-servlet map like this in web.xml.
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/login</url-pattern>
<url-pattern>/logout</url-pattern>
<url-pattern>/index</url-pattern>
<url-pattern>/cars/*</url-pattern>
<url-pattern>/people/*</url-pattern>
</servlet-mapping>
And my dispatcher-servlet.xml map like:
<bean id="urlMap"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<props>
<prop key="/login">loginController</prop>
<prop key="/logout">logoutController</prop>
<prop key="/index">welcomeController</prop>
<prop key="/cars">listCarsController</prop>
<prop key="/cars/">listCarsController</prop>
<prop key="/cars/*">showCarController</prop>
<prop key="/cars/*/action">actionCarController</prop>
<prop key="/people">listPeopleController</prop>
<prop key="/people/">listPeopleController</prop>
<prop key="/people/*">showPersonController</prop>
<prop key="/people/*/action">actionPersonController</prop>
</props>
</property>
</bean>
This is not working as expected, since when I visit for example /people/1/action, The Dispacher servlet servlet says looking handler for [1/action] and of course it is not as search but with the people/ in front to separate the peoples id to the cars id.
Is like the '*' would be the only URL entered.
By the way I want to keep my URL's extension less, I know that to map the dispatcher to *.htm on the web.xml file, would have taken care of the problem.
But its got to be a way to map the dispatche or to make the dispatcher search for a full url definition.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以设置
alwaysUseFullPath
属性:或者,您可以为调度程序 servlet 设置单个
。由于它不能是/*
,因此您需要 使用 UrlRewriteFilter。You may set a
alwaysUseFullPath
property:Alternatively, you may set a single
<url-pattern>
for the dispatcher servlet. Since it can't be/*
, you need to use UrlRewriteFilter.