org.springframework.web.servlet.PageNotFound - No

2019-05-05 22:29发布

Im new to spring mvc, earlier I accessed homepage via root package name "localhost/spring", and I cant find out what i'd changed that i got this error. Web.xml:

    <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>

Homepage Controller

@Controller
public class HomeController {

private UserServiceImpl userService;

@Inject
public HomeController(UserServiceImpl userService)
{
    this.userService = userService;
}   

@RequestMapping(value="/")
public String home(Model model) {

    System.out.println("Sdfsd");

    model.addAttribute("users", userService.getUsers());

    return "home";
}

}

servlet-context.xml:

    <annotation-driven />


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

<context:component-scan base-package="by.mvc.dao" />

<beans:bean id="dataSource"
     class="org.apache.commons.dbcp.BasicDataSource"> 
    <beans:property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <beans:property name="url" value="jdbc:hsqldb:hsql://localhost/user_db" /> 
    <beans:property name="username" value="root" />
    <beans:property name="password" value="root" />
    <beans:property name="initialSize" value="5" />
    <beans:property name="maxActive" value="10" />
</beans:bean>

<beans:bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <beans:property name="dataSource" ref="dataSource" />
    <beans:property name="packagesToScan" value="by.mvc.dao" />
    <beans:property name="hibernateProperties"> 
        <beans:props> 
            <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</beans:prop> 
        </beans:props> 
    </beans:property> 
</beans:bean>

<beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <beans:property name="sessionFactory" ref="sessionFactory"/>
</beans:bean>

<beans:bean class="org.springframework.web.servlet.view.tiles2.TilesViewResolver"/>

<beans:bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <beans:property name="definitions">
        <beans:list>
            <beans:value>/WEB-INF/views/views.xml</beans:value>
        </beans:list>
    </beans:property>
</beans:bean>

Any ideas? thanks

3条回答
Evening l夕情丶
2楼-- · 2019-05-05 23:14

I believe the application may not be scanning the package that contains the controllers. I see a dao package being scanned but I assume you wouldn't put your controllers there. In your configuration scan the package with the controllers.

<context:component-scan base-package="by.mvc.spring" />
查看更多
Lonely孤独者°
3楼-- · 2019-05-05 23:16

If you are using the <annotation-driven /> and you have one o more packages using @RequestMapping, @Controller you have to set the tag <context:component-scan base-package="com.mvc.example" /> to explicity indicates the places to scan the controllers and that's all.

Good luck!

查看更多
放荡不羁爱自由
4楼-- · 2019-05-05 23:28

If you are using tags like @RequestMapping, @Controller, etc, you have to use <mvc:annotation-driven /> instead of <annotation-driven />.

Here you have more information about it.

查看更多
登录 后发表回答