2016-01-02 32 views
0

项目结构 enter image description here在DispatcherServlet的发现HTTP请求与URI [/你好]名为 'MVC的调度'

的web.xml

<web-app version="2.5" 
     xmlns="http://java.sun.com/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

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

    <servlet> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>classpath:applicationContext.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

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

</web-app> 

MVC-调度员的servlet没有映射.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

    <mvc:annotation-driven/> 
    <mvc:default-servlet-handler/> 

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

</beans> 

的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans 
     xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

    <context:annotation-config /> 
    <context:component-scan base-package="sse.jason"/> 

    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> 

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" > 
     <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
     <property name="url" value="jdbc:mysql://localhost:3306/library?useUnicode=true&amp;characterEncoding=UTF-8"/> 
     <property name="username" value="root"/> 
     <property name="password" value="root"/> 
     <property name="initialSize" value="1"/> 
     <property name="minIdle" value="1"/> 
     <property name="maxIdle" value="30"/> 
     <property name="maxActive" value="60"/> 
     <property name="testOnBorrow" value="true" /> 
     <property name="testOnReturn" value="true" /> 
     <property name="testWhileIdle" value="true" /> 
    </bean> 

    <bean id="sessionFactory" 
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="packagesToScan"> 
      <list> 
       <value>sse.jason.model</value> 
      </list> 
     </property> 
     <property name="hibernateProperties"> 
      <value> 
       hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect 
       hibernate.hbm2ddl.auto=update 
       hibernate.show_sql=false 
       hibernate.format_sql=false 
      </value> 
     </property> 
    </bean> 
    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 
    <tx:annotation-driven transaction-manager="txManager"/> 
</beans> 

HelloContoller.java

@Controller 
public class HelloController { 
    @RequestMapping("/") 
    public String printWelcome(ModelMap model) { 
     model.addAttribute("message", "Hello world!"); 
     return "hello"; 
    } 

} 

LoginController.java

@Controller 
public class LoginController { 

    @Autowired 
    @Qualifier("iUserService") 
    private UserService iUserService; 

    @RequestMapping("/log") 
    public String loginPage() { 
     return "login"; 
    } 

    @RequestMapping("/loginCheck") 
    public ModelAndView login(HttpServletRequest request,LoginCommand command) { 
     User user = iUserService.login(command.getUsername(),command.getPassword()); 
     if (user==null) { 
      return new ModelAndView("error"); 
     } else { 
      user.setLastIp(request.getRemoteAddr()); 
      user.setLasVisit(new Date()); 
      request.getSession().setAttribute("user",user); 
      return new ModelAndView("index"); 
     } 
     //return new ModelAndView("index"); 
    } 

} 

编译结果

Jan 02, 2016 12:15:38 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler 
Info: Root mapping to handler 'helloController' 
Jan 02, 2016 12:15:38 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler 
Info: Mapped URL path [/log] onto handler 'loginController' 
Jan 02, 2016 12:15:38 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler 
Info: Mapped URL path [/log.*] onto handler 'loginController' 
Jan 02, 2016 12:15:38 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler 
Info: Mapped URL path [/log/] onto handler 'loginController' 
Jan 02, 2016 12:15:38 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler 
Info: Mapped URL path [/loginCheck] onto handler 'loginController' 
Jan 02, 2016 12:15:38 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler 
Info: Mapped URL path [/loginCheck.*] onto handler 'loginController' 
Jan 02, 2016 12:15:38 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler 
Info: Mapped URL path [/loginCheck/] onto handler 'loginController' 

错误

localhost:8080 404 error 

warnings: No mapping found for HTTP request with URI [/hello] in DispatcherServlet with name 'mvc-dispatcher' 
Jan 02, 2016 12:15:40 org.springframework.web.servlet.PageNotFound noHandlerFound 

localhost:8080/log no response 

会有人可以给我一种帮助吗?非常感谢。

+1

你在哪里有'/ hello'端点控制器注册? –

+0

你测试的URL是什么,你在控制器中指定了'/ hello'映射的位置? –

+0

问题是什么? –

回答

-1

您映射为hello.jsp页面/在HelloController中,如果你希望它是在/hello担任修改它的代码如下

@RequestMapping("/hello")//instead of @RequestMapping("/") 
public String printWelcome(ModelMap model) { 
    model.addAttribute("message", "Hello world!"); 
    return "hello"; 
} 
相关问题