web.xml 注意,因为golfing-servlet.xml并不是ContextLoaderListener
默认寻找的applicationContext.xml,所以要明确指定
contextConfigLocation的参数
If you don't specify the contextConfigLocation
context parameter, the
ContextLoaderListener
will look for a file called
/WEB-INF/applicationContext.xml
to load.
<web-app>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/golfing-*.xml</param-value>
</context-param>
<servlet>
<servlet-name>golfing</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>golfing</servlet-name>
<url-pattern>/test/*</url-pattern>
</servlet-mapping>
</web-app>
gofing-servlet.xml 注意:该xml名是根据web.xml的servlet-name写的
With the above servlet configuration in place, you will need to have a file called
/WEB-INF/
golfing-servlet.xml
in your application; this file will contain all of your Spring Web MVC-specific components (beans).
<mvc:annotation-driven/>是要加的,否则无法把你请求的url转向对应的@controller.
This tag registers the DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter beans that are required for Spring MVC to dispatch requests to @Controllers.
<?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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<context:component-scan base-package="com.yuxuan"/>
<mvc:annotation-driven/>
<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/page/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
controller,注意RequestMapping的value参数"/form"不能写成“/test/form",因为/test已经映射到DispatcherServlet
package com.yuxuan.cotroller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class BaseController {
@RequestMapping(value="/form",method=RequestMethod.GET)
public ModelAndView toHelloWorld(){
return new ModelAndView("/helloworld");
}
}
helloworld.jsp,该/WEB-INF/page/ 里面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
hello
</body>
</html>