Servlet 映射帮助 - 可以避免引用上下文名称吗?
我正在使用 Tomcat 6 和 Spring 2.5 开发 Spring 应用程序。我正在尝试让我的 URL 映射正确。我想要做的工作如下:
http://localhost:8080/idptest -> doesn't work
但是,我必须引用 URL 中的上下文名称才能解析映射:
http://localhost:8080/<context_name>/idptest -> works
如何避免在不使用重写/的情况下引用 URL 中的上下文名称的要求代理引擎例如Apache?
这是我的 web.xml 中的 servlet 定义和映射:
<servlet>
<servlet-name>idptest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/conf/idptest.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>idptest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
这是我的控制器的轮廓(显示请求映射的注释):
@Controller
@RequestMapping("/idptest")
public class MyController {
@RequestMapping(method=RequestMethod.GET)
public String setupForm(Model model){
MyObject someObject = new MyObject();
model.addAttribute("someObject", someObject);
return "myform";
}
@RequestMapping(method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("someObject") MyObject someObject) throws Exception {
// POST logic...
}
}
谢谢!
I am working on a Spring application using Tomcat 6 and Spring 2.5. I'm trying to get my URL mapping correct. What I would like to have work is the following:
http://localhost:8080/idptest -> doesn't work
But instead, I have to reference the context name in my URL in order to resolve the mapping:
http://localhost:8080/<context_name>/idptest -> works
How can I avoid the requirement of referencing the context name in my URL without using a rewrite/proxy engine e.g. Apache?
Here is the servlet definition and mapping from my web.xml:
<servlet>
<servlet-name>idptest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/conf/idptest.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>idptest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Here's the outline of my controller (showing annotations for request mappings):
@Controller
@RequestMapping("/idptest")
public class MyController {
@RequestMapping(method=RequestMethod.GET)
public String setupForm(Model model){
MyObject someObject = new MyObject();
model.addAttribute("someObject", someObject);
return "myform";
}
@RequestMapping(method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("someObject") MyObject someObject) throws Exception {
// POST logic...
}
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将取决于您的 servlet 容器,对于 Tomcat - 您几乎必须将您的 web 应用程序部署为 ROOT web 应用程序,即在 $CATALINA_HOME/webapps/ROOT/ 下
更多信息 此处
That's going to depend on your servlet container, for Tomcat - you pretty much have to deploy your webapp as the ROOT webapp, that is, under $CATALINA_HOME/webapps/ROOT/
More info here
只需将您的 war 文件重命名为 ROOT.war,然后应用程序就会在根上下文中运行(即上下文名称为空)
Just rename your war file to ROOT.war, then the application runs in root context (i.e. with empty context name)