我正在尝试编写一个Web应用程序。我已经用hibernate建立了一个数据库,我用3层写了应用程序 - dao,service和controller。项目编译,但当我在浏览器中打开网址时,我有 - HTTP状态404 - / ticket-system / person / home。我没有例外。我将在这里复制代码。
当我尝试在eclipse中打开 http://localhost:8080/ticket-system/prson/home 时,我发出此警告 - 警告:找不到带URI的HTTP请求的映射[/ ticket-system / prson / home]在DispatcherServlet中,名称为“app-config”
控制器
@Controller
@RequestMapping("/person")
public class PersonController {
@Autowired
private PersonService personService;
@RequestMapping(value = "/home")
public ModelAndView home() {
List<Person> listUsers = personService.list();
ModelAndView model = new ModelAndView("home");
model.addObject("personList", listUsers);
return model;
}
}
DaoImpl
@Repository("personDao")
public class PersonDaoImpl implements PersonDao {
@Autowired
private SessionFactory sessionFactory;
@Override
@Transactional
public List<Person> list() {
@SuppressWarnings("unchecked")
List<Person> listUser = (List<Person>) sessionFactory.getCurrentSession().createCriteria(Person.class)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
return listUser;
}
}
ServiceImpl
@Service("personService")
public class PersonServiceImpl implements PersonService {
@Autowired
private PersonDao personDao;
@Override
public List<Person> list() {
return personDao.list();
}
}
应用-config.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="com.ticket" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/ticket_system" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.ticket.model.Person</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<servlet>
<servlet-name>app-config</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/app-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app-config</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
针对home.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home</title>
</head>
<body>
<body>
<div align="center">
<h1>Person List</h1>
<table border="1">
<th>id</th>
<th>fname</th>
<th>lname</th>
<th>password</th>
<th>email</th>
<th>nickName</th>
<c:forEach var="person" items="${personList}" varStatus="status">
<tr>
<td>${status.index + 1}</td>
<td>${person.fname}</td>
<td>${person.lname}</td>
<td>${person.password}</td>
<td>${person.email}</td>
<td>${person.nickName}</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</body>
</html>
Rahul建议之后编辑
III 01, 2016 1:37:23 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:ticket-system' did not find a matching property.
III 01, 2016 1:37:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version: Apache Tomcat/8.0.30
III 01, 2016 1:37:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Dec 1 2015 22:30:46 UTC
III 01, 2016 1:37:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number: 8.0.30.0
III 01, 2016 1:37:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Windows 10
III 01, 2016 1:37:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 10.0
III 01, 2016 1:37:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: amd64
III 01, 2016 1:37:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: C:\Program Files\Java\jdk1.8.0_66\jre
III 01, 2016 1:37:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.8.0_66-b18
III 01, 2016 1:37:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
III 01, 2016 1:37:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
III 01, 2016 1:37:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: D:\apache-tomcat-8.0.30
III 01, 2016 1:37:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
III 01, 2016 1:37:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=D:\apache-tomcat-8.0.30
III 01, 2016 1:37:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
III 01, 2016 1:37:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=D:\apache-tomcat-8.0.30\endorsed
III 01, 2016 1:37:23 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1251
III 01, 2016 1:37:23 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.8.0_66\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_73/bin/server;C:/Program Files/Java/jre1.8.0_73/bin;C:/Program Files/Java/jre1.8.0_73/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\SlikSvn\bin;C:\Program Files\Java\jdk1.8.0_66\bin;C:\Program Files (x86)\Skype\Phone\;D:\eclipse;;.
III 01, 2016 1:37:23 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
III 01, 2016 1:37:23 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
III 01, 2016 1:37:23 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
III 01, 2016 1:37:23 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
III 01, 2016 1:37:23 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 396 ms
III 01, 2016 1:37:23 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
III 01, 2016 1:37:23 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.30
III 01, 2016 1:37:24 PM org.apache.catalina.core.ContainerBase startInternal
SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/ticket-system]]
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:916)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:871)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/ticket-system]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
... 6 more
Caused by: java.lang.IllegalArgumentException: Invalid <url-pattern> * in servlet mapping
at org.apache.catalina.core.StandardContext.addServletMapping(StandardContext.java:3160)
at org.apache.catalina.core.StandardContext.addServletMapping(StandardContext.java:3135)
at org.apache.catalina.startup.ContextConfig.configureContext(ContextConfig.java:1372)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1176)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:771)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:305)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:95)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5154)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 6 more
III 01, 2016 1:37:24 PM org.apache.catalina.core.ContainerBase startInternal
SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:916)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:441)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:769)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.startup.Catalina.start(Catalina.java:625)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:351)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:485)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:924)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:871)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 6 more
III 01, 2016 1:37:24 PM org.apache.catalina.startup.Catalina start
SEVERE: The required Server component failed to start so Tomcat is unable to start.
org.apache.catalina.LifecycleException: Failed to start component [StandardServer[8005]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.startup.Catalina.start(Catalina.java:625)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:351)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:485)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardService[Catalina]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:769)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 7 more
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:441)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 9 more
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:924)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 11 more
III 01, 2016 1:37:24 PM org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["http-nio-8080"]
III 01, 2016 1:37:24 PM org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["ajp-nio-8009"]
III 01, 2016 1:37:24 PM org.apache.catalina.core.StandardService stopInternal
INFO: Stopping service Catalina
III 01, 2016 1:37:24 PM org.apache.coyote.AbstractProtocol destroy
INFO: Destroying ProtocolHandler ["http-nio-8080"]
III 01, 2016 1:37:24 PM org.apache.coyote.AbstractProtocol destroy
SEVERE: Failed to destroy end point associated with ProtocolHandler ["http-nio-8080"]
java.lang.NullPointerException
at org.apache.tomcat.util.net.NioEndpoint.releaseCaches(NioEndpoint.java:305)
at org.apache.tomcat.util.net.NioEndpoint.unbind(NioEndpoint.java:481)
at org.apache.tomcat.util.net.AbstractEndpoint.destroy(AbstractEndpoint.java:823)
at org.apache.coyote.AbstractProtocol.destroy(AbstractProtocol.java:533)
at org.apache.catalina.connector.Connector.destroyInternal(Connector.java:1023)
at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:305)
at org.apache.catalina.core.StandardService.destroyInternal(StandardService.java:589)
at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:305)
at org.apache.catalina.core.StandardServer.destroyInternal(StandardServer.java:859)
at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:305)
at org.apache.catalina.startup.Catalina.start(Catalina.java:629)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:351)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:485)
III 01, 2016 1:37:24 PM org.apache.coyote.AbstractProtocol destroy
INFO: Destroying ProtocolHandler ["ajp-nio-8009"]
III 01, 2016 1:37:24 PM org.apache.coyote.AbstractProtocol destroy
SEVERE: Failed to destroy end point associated with ProtocolHandler ["ajp-nio-8009"]
java.lang.NullPointerException
at org.apache.tomcat.util.net.NioEndpoint.releaseCaches(NioEndpoint.java:305)
at org.apache.tomcat.util.net.NioEndpoint.unbind(NioEndpoint.java:481)
at org.apache.tomcat.util.net.AbstractEndpoint.destroy(AbstractEndpoint.java:823)
at org.apache.coyote.AbstractProtocol.destroy(AbstractProtocol.java:533)
at org.apache.catalina.connector.Connector.destroyInternal(Connector.java:1023)
at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:305)
at org.apache.catalina.core.StandardService.destroyInternal(StandardService.java:589)
at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:305)
at org.apache.catalina.core.StandardServer.destroyInternal(StandardServer.java:859)
at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:305)
at org.apache.catalina.startup.Catalina.start(Catalina.java:629)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:351)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:485)
答案 0 :(得分:1)
/ticket-system/person/home
未映射到任何网址,因为您在下面的web.xml中提到了这一点:
<servlet-mapping>
<servlet-name>app-config</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
将其更改为
<servlet-mapping>
<servlet-name>app-config</servlet-name>
<url-pattern>*</url-pattern>
</servlet-mapping>
答案 1 :(得分:0)
您的servlet名为app-config
,配置文件的名称为app-config.xml
。默认情况下,Spring正在类路径中查找<servlet-name>-servlet.xml
文件。因此,将配置文件重命名为app-config-servlet.xml
然后将servlet-mapping
更改为
<servlet-mapping>
<servlet-name>app-config</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
此外,您应在@RequestMapping
中指定HTTP方法
@RequestMapping(value = "/home", method = RequestMethod.GET)
public ModelAndView home() {
List<Person> listUsers = personService.list();
ModelAndView model = new ModelAndView("home");
model.addObject("personList", listUsers);
return model;
}
然后尝试访问http://localhost:8080/ticket-system/person/home
应用程序的上下文根是ticket-system
对吗?