有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring MVC:调度程序映射到错误的视图

问题是:我把客户作为我的观点。根据我的viewresolver,应该映射到WEB-INF/pages/customer。html。而是通过dispatcher servlet,无法找到客户html。它给出的错误是:“警告:在名为‘mvc dispatcher’的DispatcherServlet中找不到URI为[/SpringMVC/WEB-INF/pages/customer.html]的HTTP请求的映射”

这是我的控制器

@Controller
public class CustomerController implements BeanFactoryAware {

    private Customers customers;


    /*public String getCustomer(@RequestParam String name) {

        //ApplicationContext context = new FileSystemXmlApplicationContext("/WEB-INF/springapp-servlet.xml");
        //Customers customers = get
        System.out.println("In Controller");
        return "customer";
    }*/

    @RequestMapping(value="/form")  
    public String getCustomer(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {


            System.out.println("In Customer Controller");
            return "customer";
    }

    @Override
    public void setBeanFactory(BeanFactory context) throws BeansException {
        // TODO Auto-generated method stub
        customers = (Customers)context.getBean("customers");

        //System.out.println(customers);

    }

}

这是我的网站。xml

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd“>

<display-name>Spring Web MVC Application</display-name>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml,/WEB-INF/beans.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

这是我的调度员。xml


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

共 (3) 个答案

  1. # 1 楼答案

    这将不起作用,因为最终调用将转到RequestDispatcher,如下所示:

    RequestDispatcher dispatcher = httpRequest.getRequestDispatcher("/WEB-INF/pages/test.html");
    dispatcher.forward(httpRequest, httpRequest);
    

    此时,容器将期望DispatcherServlet再次处理请求(因为/*路径)。如果它是一个jsp页面,那么容器有一个*.jsp的映射,并且知道如何处理它

    解决方法是将资源放置在相对于Web资源的某个位置(如果是maven结构,则在src/main/webapp/resources/下,为该内容配置处理程序:

    <mvc:resources location="/resources/" mapping="/resources/**" />
    

    现在,您可以从控制器返回:

    return "forward:/resources/mypage.html";
    

    此外,我看到您正在查找“customers”bean,您不需要这样做,而是希望Spring将其注入:

    @Controller
    public class CustomerController{
    
        @Autowired private Customers customers;
    
  2. # 2 楼答案

    尝试从以下位置更改servlet映射:

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
  3. # 3 楼答案

    您的servlet将拦截对匹配“/*”的url模式的所有调用,但它无法在任何spring控制器中找到指定的映射

    我曾经遇到过类似的问题,而我是如何克服这一问题的,是通过遵循您所有spring调用的特定模式。e、 g

    <url-pattern>*.htm</url-pattern>
    

    希望这对你有帮助