我已经编写了一个基本的SpringMVC代码,但是当我应该运行该项目时,我遇到了一些服务器错误(我使用的是Tomcat 8.0服务器),下面是我的代码
的web.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" 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>SpringDemo</display-name>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet <!-- redirect to front controller -->
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern> <!-- maps incoming URL into DispatcherServlet -->
</servlet-mapping>
</web-app>
弹簧调度-servlet.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- FrontController:it's job is to response back to client -->
<bean id="HandlerMapping"
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> <!-- HandlerMapping:Scans incoming URL pattern and maps into respective Controller -->
<bean name="/welcome.html"
class="com.pack1.hellocontroller.HelloController" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value> <!-- sends back responses to the client -->
</property>
</bean>
</beans>
HelloController.java
package com.pack1.hellocontroller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController; //AbstractController:basic controller class from springmvc
public class HelloController extends AbstractController{
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView modelandview = new ModelAndView("HelloPage"); //viewresolver name:HelloPage
modelandview.addObject("msg", "hello, it's my first springmvc application");
return modelandview; //returns java object to the FrontController
}
}
HelloPage.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>SpringMVC</h1>
<h2>${msg}</h2>
</body>
</html>
答案 0 :(得分:0)
像这样更改 spring-dispatcher-servlet.xml :
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- FrontController:it's job is to response back to client -->
<context:component-scan base-package = "com.pack1" />
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/WEB-INF/" />
<property name = "suffix" value = ".jsp" />
</bean>
</beans>
确保您的jsp文件位于 WEB-INF 文件夹
中现在在您的控制器中:
@Controller
@RequestMapping("/welcome")
public class HelloController{
@RequestMapping(method=RequestMethod.GET)
public ModelAndView welcome(){
return new ModelAndView("HelloPage", "msg", "Hello Spring MVC Framework");
}
}
此致