无法将dispatcherServlet映射到上下文根 [英] Can't map dispatcherServlet to context root

查看:236
本文介绍了无法将dispatcherServlet映射到上下文根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用于spring mvc的当前配置:

this is the current configuration i am using for spring mvc:

1- web.xml:

<servlet>  
    <servlet-name>spring</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/config/dispatcherServlet.xml</param-value>
   </init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>  
    <servlet-name>spring</servlet-name>  
    <url-pattern>/</url-pattern>  
</servlet-mapping>

2- dispatcherServlet.xml:

<context:component-scan base-package="com.app" />
 <context:annotation-config />
<mvc:annotation-driven />   
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"/>
    <property name="suffix" value=".jsp"/>
</bean>

3-控制器:我的网页直接位于webapp文件夹下

@Controller
public class SearchController {

private Log log = LogFactory.getLog(getClass());

@RequestMapping("/search.jsp")
public String search(Model model, HttpServletRequest request,
        HttpSession session) {
          log.debug("Search Controller");
          return "search";
 }

问题:当尝试按以下方式访问搜索页面时:

ISSUE: when trying to access the search page as follows:

http://localhost:8080/MyAPP/search.jsp

不会调用控制器,但是当我将调度程序servlet映射到/mapping/*并按以下方式访问搜索页面时:

the controller is not invoked, but when i was mapping the dispatcher servlet to /mapping/* and accessing the search page as follows:

http://localhost:8080/MyAPP/mapping/search.jsp

控制器已正确调用,我使用的是 spring 3.0.5.RELEASE .

the controller was invoked correctly, i am using spring 3.0.5.RELEASE.

请告知,谢谢.

推荐答案

我认为您忘记了在Web服务器/servlet容器中配置的内置默认servlet.例如,在Tomcat7/conf/web.xml中存在:

I think you are forgetting about the built in default servlet configured in your web server/servlet container. For example in Tomcat7/conf/web.xml there exists:

<servlet>
    <servlet-name>jsp</servlet-name>
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
    <init-param>
        <param-name>fork</param-name>
        <param-value>false</param-value>
    </init-param>
    <init-param>
        <param-name>xpoweredBy</param-name>
        <param-value>false</param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<!-- The mappings for the JSP servlet -->
<servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.jsp</url-pattern>
    <url-pattern>*.jspx</url-pattern>
</servlet-mapping>

在到达Spring之前正在捕获*.jsp.我通过删除所有Spring配置在本地进行了测试,并且可以 still 获得search.jsp.

which is catching the *.jsp before it ever gets to Spring. I tested this locally by removing all of the Spring configuration and could still get the search.jsp.

DefaultAnnotationHandlerMapping的工作原理在解释其工作原理时应该很有用.

How DefaultAnnotationHandlerMapping works should be useful in explaining why this works they way it does.

使用<url-pattern>/mapping/*</url-pattern>时,您创建的匹配比简单的/更具体,因此默认Servlet(即Tomcat)Servlet将忽略请求,并将其路由到正确配置的控制器.

When you had <url-pattern>/mapping/*</url-pattern> you created a more specific match than the simple / so requests were ignored by the default (i.e. Tomcat) servlet and routed to your correctly configured controller.

解决此问题的一种方法是使用<url-pattern>/*</url-pattern>强制通过servlet进行所有操作,但是您还需要进行一些其他更改以避免映射分辨率问题.

One way to fix this is to force everything through your servlet by using <url-pattern>/*</url-pattern> but you will also need to make a few other changes to avoid mapping resolution problems.

我将* .jsp文件移到(标准?)子目录/WEB-INF中并添加了

I moved the *.jsp files into the (standard?) subdirectory /WEB-INF and added

<servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>/WEB-INF/*</url-pattern>
</servlet-mapping>

更改为 web.xml ,并将 dispatcherServlet.xml 更改为如下所示:

to web.xml and changed dispatcherServlet.xml to match like so:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/"/>
    <property name="suffix" value=".jsp"/>
</bean>

如果不进行这些更改,则对/search.jsp的请求将由您配置为/search.jsp将Tomcat发送到无限转发循环中的InternalResourceViewResolver来解决!

If you do not make these changes, a request to /search.jsp would be resolved by the InternalResourceViewResolver you configured to /search.jsp sending Tomcat into an infinite forwarding loop!

未找到映射带有URI [/WEB-INF/pages/apiForm.jsp] 的HTTP请求在这里可能有用.

No mapping found for HTTP request with URI [/WEB-INF/pages/apiForm.jsp] may be useful here.

此外:对于大多数我的Spring XML配置项目,我使用/WEB-INF/views将视图层与/WEB-INF根目录中的任何配置分开.

Aside: For most of my Spring XML configured projects I use /WEB-INF/views to keep the view layer separate from any configuration in the /WEB-INF root.

这篇关于无法将dispatcherServlet映射到上下文根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆