我试过简单的你好世界程序使用spring.It不按预期工作。春天你好,世界不工作404错误
package com.Spring;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class Hello {
@RequestMapping(value="/hello",method=RequestMethod.GET)
public String helloWorld(ModelMap model){
model.addAttribute("message", "hello world");
return "hello";
}
}
HelloWeb-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.Spring" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"/>
</bean>
</beans>
的web.xml
<web-app id="WebApp_ID" 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/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>com.Spring.Hello</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
的hello.jsp文件。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${message}
</body>
</html>
我已经使用Ant生成WAR文件并部署deployed.After war文件没有在terminal.Now我有尝试在浏览器访问URL没有错误时显示404错误。
http://ipaddress:8080/SpringHello/hello.jsp
如何访问URL?或者我做错了什么?
尝试:http:// ipaddress:8080/SpringHello/hello。 – gregory561
谢谢reply.yes.I已尝试不使用.jsp.Again它显示相同的错误404. – Ami
请确保您的jsp和Web-servlet.xml位于WEB-INF下的jsp文件夹中。它不起作用,请尝试从Web-servlet.xml和web.xml中删除WEB-INF条目,并将jsp和Web-servlet.xml放入webapps文件夹direclty中。 – jsjunkie