新建一个java web工程,然后 单击右键,Add Frameworkds中选择spring mvc,平台会自动帮我们新增sping mvc的支持。完成后,会在WEB-INF目录下新建两个文件:applicationContext.xml和dispatcher-servlet.xml。另外在web.xml中会追加一堆内容,如下截图。红框中是唯一改动。

applicationContext.xml不用管。dispacher-servlet.xml是负责完成spring框架内部的路径和controller类的映射。上图红框改成“/”表示所有的路径都交给spring去匹配对应的controller来处理。
现在dispatcher-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
有两种路径和controller的映射方式。
方式一,在dispatcher-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="/test" class="controller.MyController" />
</beans>
MyController的实现如下:
package controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyControllerimplements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)throws Exception {
System.out.println("MyController");
return null;
}
}
当在浏览器里面敲击http://localhost:8080/test 后, 服务器打印出 MyController 的 日志,表示被重定向 由MyController的handleRequest来托管了。
方式二,自动方式。 即 类通过注解的方式匹配上对应的路径。如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 自动扫描匹配controller下的类以匹配 路径 -->
<context:component-scan base-package="controller" />
<!-- 静态资源(js、image等)的访问 -->
<mvc:default-servlet-handler/>
<!-- 开启注解 -->
<mvc:annotation-driven/>
<!--ViewResolver 视图解析器-->
<!--用于支持Servlet、JSP视图解析-->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
类通过注解来匹配路径如下:
@Controller
public class SimpleController {
@RequestMapping(value ="/hello", method = RequestMethod.GET)
public String printHello(ModelMap model) {
System.out.println("print hello");
model.addAttribute("msg","Spring MVC Hello World");
return "hello";
}
}
然后发布。
在浏览器中输入http://localhost:8080/hello ,则被重定向到hello.jsp, 变量也一并传送到hello.jsp
网友评论