Spring MVC环境搭建和配置(多动作控制器MultiActionController)
引言:本文介绍了Spring MVC的简单搭建,文章中的代码注释部分是扩展内容,可以不用编写,但可以参考参考.
一、 jar包引入:
Spring 2.5.6: spring.jar、spring-webmvc.jar、commons-logging.jar、cglib-nodep-2.1_3.jar
二、 web.xml配置:
<!-- Spring MVC配置 --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml --> <!-- <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> //默认时的配置 </init-param> --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> //后缀名可以自定义 </servlet-mapping> <!-- Spring配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
三、spring-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:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> <!---如果TestActionController类实现的是Controller接口(单动作控制器),则spring-servlet.xml里面只需此配置就OK --> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="test.do">testActionController</prop> <!-- <prop key="/test.do">testActionController</prop> --> </props> </property> </bean> <!-- 如果applicationContext.xml里面已经对TestActionController类注入了,那就不需要再在这里重复注入了 --> <bean id="testActionController" class="com.test.TestActionController"> <property name="methodNameResolver"> <ref bean="paraMethodResolver"/> </property> <!--viewName属性将被依赖注入到TestActionController类,index为要转向的jsp页面名称(此处可以不用配置,直接在TestActionController里面指定)--> <!-- <property name="viewName"> <value>index</value> </property> --> </bean> <!--如果TestActionController类实现Controller接口(单动作控制器),那么此处不需要配置--> <bean id="paraMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <!--此处的method为要访问的TestActionController类方法的方法参数名--> <property name="paramName" value="method"/> </bean> <!-- 对转向页面的路径解析。prefix:前缀,suffix:后缀. (此处可以不用配置,直接在TestActionController类里面指定要访问的页面)--> <!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass"> <value>org.springframework.web.servlet.view.InternalResourceView</value> </property> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> --> </beans>
四、建立TestActionController类,继承MultiActionController类:
注意:1:MultiActionController类为多动作控制器,就是多个不同的请求可以访问同一个controller类(相当于Servlet,不过一个Servlet只能处理一个请求),例如下面的TestActionController类里面就有insert,update,delete三个方法;
2:这里还可以通过实现Controller接口处理请求,不过Controller接口是单动作控制器,跟Servlet一样,只能处理一个请求,当有多个不同的请求时就需要建立多个不同的Servlet类(实现Controller接口时请注意下spring-servlet.xml配置).
public class TestActionController extends MultiActionController { private Logger logger=Logger.getLogger(this.getClass().getName()); //依赖注入一个名为viewName的参数,例如一个JSP文件,作为展示model的视图 // private String viewName; // public String getViewName (){ // return this.viewName; // } // public void setViewName (String viewName){ // this. viewName =viewName; // } public ModelAndView insert(HttpServletRequest request, HttpServletResponse response) throws ServletRequestBindingException, IOException { ModelAndView view = new ModelAndView("index.jsp"); view.addObject("info","新增数据..."); return view; } public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws ServletRequestBindingException, IOException { Map map = new HashMap(); map.put("info", "删除数据..."); //如果JSP页面是依赖注入进来的,那这里通过getViewName()拿到页面名称,否则直接return new ModelAndView("index.jsp",map); return new ModelAndView(getViewName(),map); } public ModelAndView update(HttpServletRequest request, HttpServletResponse response) throws ServletRequestBindingException, IOException { PrintWriter out = response.getWriter(); String data = "{\"info\":\"修改数据...\",\"flag\":true}"; out.print(data); out.close(); //异步请求时这里返回null return null; } }
五、建立index.jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head>TestActionController示例</head> <body> ${info} ${map.info} </body> </html>
六、测试,在浏览器url地址栏输入(如果实现的是Controller接口,则请求地址为http://localhost:8080/test.do):
http://localhost:8080/test.do?method=insert //将访问TestActionController类的insert方法
http://localhost:8080/test.do?method=delete //将访问TestActionController类的delete方法