标签:
本文主要讲的是控制器,Action继承什么类。记得Springmvc系列的第一篇文章说过。SpirngMVC的实现流程。
这里分为两种1.实现接口。2.继承类。我们知道就是因为SpirngMvc有这么多五花八门的方式,不像Struts2那样,只继承一个SupportAction那么形式单一,所以才出现了适配器,去寻找Action. 我们之前讲的都是实现接口的方式(implements Controller),但是这里我们要讲的是Action去实现(控制器),主要讲两个控制器类。
为什么要引出控制器呢?以前的实现接口的方式有什么弊端吗?
有弊端:1.Action类实现了Controller接口,带来了代码的耦合,
2.如果参数过多,实现接口的Action,收集起来不方便。
第一个控制器类:jsp到jsp的控制器_ParameterizableViewController
这个控制器的功能是jsp页面直接转到jsp页面,中间不需要走Action.
注意点:
1.很明显,因为是jsp页面直接转到jsp页面,所以就不需要写Action。
2.没有了Action.自然适配器也不需要写了,映射器(根据url请求去找对应的Action),视图解析器都不用写了。
3.引入一个org.springframework.web.servlet.mvc.parameterizanleViewController.
案例如下:
这个案例的目的:在index.xml中有一个按钮,点击之后进入到/WEB-INF/index11.jsp中。
案例的工作流程:UrL请求来到web.xml中。根据web.xml的配置,来到了springmvc_03.xml中。然后根据请求名进入到了 org.springframework.web.servlet.mvc.ParameterizableViewController,再根据<property name="viewName" value="/WEB-INF/index11.jsp"></property>
进入到了index11.jsp中。
涉及到的代码文件:
1.web.xml
2.index.xml
3.springmvc.xml和springmvc_03.xml
4.index11.xml
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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>SpringMvc_10day_self</display-name> <servlet> <!--这个名字可以随便取得,但是这个名字取了之后,以后在 WEB-INF下面创建SpirngMVC的配置文件是,命名必须以这个开头, 所以这里取名叫做DispatcherServlet,那么之后的xml文件取名必须为DispatcherServlet-servlet.xml(一个字都不能差) --> <servlet-name>DispatcherServlet</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 通知DispatcherServlet去指定目录下找到springmvc.xml配置文件 --> <!-- 注意这里的 <param-name>contextConfigLocation</param-name>一个字母都不能有错 一旦有错就会去WEB-INF下面去找 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
2.index.xml代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘index.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="${pageContext.request.contextPath}/showuser.action" method="post"> 用户的姓名:<input type="text" name="username" > <input type="submit" value="提交"> </form> <a href="${pageContext.request.contextPath}/index.action">点击我</a> </body> </html>
3.springmvc.xml和springmvc_03.xml代码如下:
springmvc.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" > <import resource="com/guigu/shen/Action3/springmvc_003.xml"/> </beans>
springmvc_03.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" > <!-- 这么配相当于index.action这个请求来到了SpringMvc给我们写好的 ParameterizableViewController中,而不是像之前一样进入大我们自己的写的Action实例中 --> <bean name="/index.action" class="org.springframework.web.servlet.mvc.ParameterizableViewController"> <!-- 转发到真实视图名,如果是逻辑名称的话,还要自己配视图解析器 --> <property name="viewName" value="/WEB-INF/index11.jsp"></property> </bean> </beans>
4.index11.xml代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘index.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> asdfafasfsadf </body> </html>
运行结果:对的。
07SpringMvc_jsp到jsp的控制器_ParameterizableViewController
标签:
原文地址:http://www.cnblogs.com/shenxiaoquan/p/5746846.html