启用S​​pring MVC处理请求

时间:2016-05-03 17:26:11

标签: spring spring-mvc web.xml

大家好,我一直在处理这个问题,我找不到解决方案,希望你可以帮助我。无论我对我的控制器,web.xml或search-customer-context.xml做了多少更改,我都会收到“HTTP Status 404 - /SearchCustomerID/search.jsp”。希望你能帮忙解决这个问题。这是我的文件。 的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>Search Customer ID</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <!-- <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/search-customer-servlet.xml</param-value>
</context-param> -->
    <servlet> 
        <servlet-name>search-customer</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>search-customer</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
     <!--<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/search-customer-context.xml</param-value>
    </context-param>
    <listener>
       <listener-class>
           org.springframework.web.context.ContextLoaderListener
       </listener-class>
    </listener>  -->
</web-app>

`

搜索客户context.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<context:component-scan base-package="com.example.*" />
</bean>
     <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp" />
    </bean>

SearchController.java

@Controller 公共类SearchController {

@RequestMapping("/SearchCustomerID/search")
public String searchInformation(@Valid @ModelAttribute("userInfo") UserInfo userInfo){
    System.out.println("Controller reached");
    System.out.println("Name: "+userInfo.getName());
    return "search";
}

}

JSP:
enter image description here

1 个答案:

答案 0 :(得分:0)

对不起大家我的不好。问题是我已经配置了web.xml,<url-pattern>设置为* html但我的请求总是* .jsp,所以显然我不匹配。现在我更改了URL,我可以通过注释为Controller的类。