gpt4 book ai didi

java - 在 tomcat 7.0 中运行 Spring MVC 应用程序给我 HTTP 状态 404 : requested resource is not available.

转载 作者:行者123 更新时间:2023-11-28 23:34:50 25 4
gpt4 key购买 nike

过去 2 天我一直在尝试对此进行调试。由于在 stackoverflow 上有许多类似的问题。但我找不到解决方案。

我正在尝试执行 Spring MVC CRUD 项目。当我运行该应用程序时,我收到了 错误 404:请求的资源 (/SpringMyPersonalDiary/user) 不可用。

我的结构如下:

enter image description here

网络.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>SpringMyPersonalDiary</display-name>

<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

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

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/</welcome-file>
<welcome-file>index</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

spring-servlet.xml是

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

<mvc:annotation-driven/>
<context:annotation-config />
<context:component-scan base-package="com.smithak" />


<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>

用户 Controller .java

@Controller
public class UserController {

@Autowired
private UserService userService;

@RequestMapping("/user")
public String setupForm(Map<String, Object> map) {
User user = new User();
map.put("user", user);
map.put("userList", userService.getAllUsers());
return "user";
}

// We might not need delete & edit
@RequestMapping(value = "/user.do", method = RequestMethod.POST)
public String doActions(@ModelAttribute User user, BindingResult result, @RequestParam String action, Map<String, Object> map) {

User userResult = new User();
switch (action.toLowerCase()) {// Only in Java 7 we can put string in
// switch statement
case "add":
userService.add(user);
userResult = user;
break;
case "delete":
userService.delete(user.getId());
userResult = new User();
break;
case "edit":
userService.edit(user);
userResult = user;
break;
case "search":
User searchedUser = userService.getUser(user.getId());
userResult = searchedUser != null ? searchedUser : new User();
break;
}
map.put("user", userResult);
map.put("userList", userService.getAllUsers());
return "user";
}
}

错误 enter image description here

任何人都可以帮我调试这个问题。任何帮助将不胜感激。谢谢。

最佳答案

我能够解决这个问题。感谢@david 帮助我调试。

我实际上在我的 src/main/resources 文件夹中丢失了 hibernate.cfg.xmllog4j.xml。通过查看我的控制台中的错误,我能够解决这个问题。错误如下:

Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [hibernate.cfg.xml] cannot be resolved to URL because it does not exist

希望这对像我这样的业余爱好者有所帮助。 :)

关于java - 在 tomcat 7.0 中运行 Spring MVC 应用程序给我 HTTP 状态 404 : requested resource is not available.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25596179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com