在名为“appServlet”的DispatcherServlet中找不到具有URI [/ test_jboss_local / welcome]的HTTP请求的映射

时间:2014-09-01 07:38:47

标签: java spring hibernate maven spring-mvc

从浏览器访问http://localhost:8080/test_jboss_local/welcome网址时出现以下错误:

No mapping found for HTTP request with URI [/test_jboss_local/welcome] in DispatcherServlet with name 'appServlet'

以下是文件:

web.xml:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        /WEB-INF/spring/root-context.xml
    </param-value>
    </context-param>

    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

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

servlet-context.xml:

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

root-context.xml:

<bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
     <tx:annotation-driven transaction-manager="transactionManager"/>
        <context:component-scan base-package="com.abc" />


    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- <property name="dataSource" ref="dataSource" /> -->
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
</bean>

HelloController.java:

@Controller
public class HelloController {

    @RequestMapping(value="/welcome", method = RequestMethod.GET)
    public String welcome(ModelMap model) {

        model.addAttribute("message", "Maven Web Project + Spring 3 MVC - welcome()");

        //Spring uses InternalResourceViewResolver and return back index.jsp
        return "index";

    }

    @RequestMapping(value="/welcome/{name}", method = RequestMethod.GET)
    public String welcomeName(@PathVariable String name, ModelMap model) {

        model.addAttribute("message", "Maven Web Project + Spring 3 MVC - " + name);
        return "index";

    }

index.jsp:

<html>
<body>
<h2>Hello World!</h2>

<h4>Message : ${message}</h1>   
</body>
</html>

我在网上检查,我无法识别错误。请帮忙。

3 个答案:

答案 0 :(得分:0)

将Servlet映射更改为:

       <servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

答案 1 :(得分:0)

尝试将此行添加到servlet-context.xml配置文件中:

<mvc:default-servlet-handler/>

答案 2 :(得分:0)

如果您的控制器不在默认包中,请添加

<context:annotation-config /> 
<context:component-scan base-package="your-controller's package" /> 

在spring-context.xml中