我使用Spring Tool Suite在eclipse中开发一个简单的Spring 3。但是当我运行时,“HTTP状态404请求的资源(/ hxj /)不可用”是返回。
在eclipse的控制台中:没有错误,但有些信息看起来很奇怪:
Jun 29, 2013 7:50:47 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(H:\My Documents\eclipse\.metadata\.plugins\org.eclipse.wst.server.core
\tmp1\wtpwebapps\Spring\WEB-INF\lib\jsp-api-2.1.jar) - jar not loaded.
Jun 29, 2013 7:50:47 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(H:\My Documents\eclipse\.metadata\.plugins\org.eclipse.wst.server.core
\tmp1\wtpwebapps\Spring\WEB-INF\lib\servlet-api-2.5.jar) - jar not loaded.
以下是我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listene-class>
</listener>
<!-- Processes application requests -->
<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>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>home.jsp</welcome-file>
</welcome-file-list>
</web-app>
java类的主要部分:HomeController.java
package com.hxj.hxj;
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
@RequestMapping(value = "/home", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat
= DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
}
servlet-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org
/schema/mvc/spring-mvc.xsd
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">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<resources mapping="/resources/**" location="/resources/" />
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.hxj.hxj" />
</beans:beans>
我不知道它为什么不起作用,因为它是一个来自spring templete项目的春季MVC项目。任何人都可以帮我解决这个问题吗?
非常感谢! 伊芙
编辑: 此外,为什么网址是:localhost:8080 / hxj / 而不是:localhost:8080 / hxj / home.jsp(或/home.do)?
由于
答案 0 :(得分:2)
Acdcjunior在评论中非常严格:
在web.xml中尝试制作:
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
而不是* .do。
你问:
此外,为什么url是:localhost:8080 / hxj /而不是:localhost:8080 / hxj / home.jsp(或/home.do)?
我希望正确的网址为:localhost:8080/hxj/home
(localhost:8080/hxj/
仅适用于web.XML中的欢迎URL映射)
这是因为那是您为控制器方法指定的请求映射。它不是* .jsp,因为在Spring中你使用url来指定应该调用哪个控制器方法,而不是直接指定jsp。 (而且它不是* .do因为这不是支柱)
答案 1 :(得分:0)
在文件HomeController.java中尝试
package com.hxj.hxj;
@Controller
@RequestMapping(value = "/home", method = RequestMethod.GET)
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
@RequestMapping( method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat
= DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
}
你的src \ main \ webapp中的放了redirect.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% response.sendRedirect("home"); %>
在web.xml中添加welcome-file-list
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
Servlet映射
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
确保/ WEB-INF / views /目录中的文件home.jsp