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&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
會有人可以給我一種幫助嗎?非常感謝。
你在哪裏有'/ hello'端點控制器註冊? –
你測試的URL是什麼,你在控制器中指定了'/ hello'映射的位置? –
問題是什麼? –