07SpringMvc_jsp到jsp的控制器_ParameterizableViewController

本文主要讲的是控制器,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>

 运行结果:对的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring MVC 的 XML 配置主要包括: 1. DispatcherServlet 配置:配置 DispatcherServlet 的初始化参数、映射 URL 等。 2. ViewResolver 配置:配置视图解析器,用于将逻辑视图名解析为具体的视图。 3. HandlerMapping 配置:配置处理器映射器,用于将请求映射为具体的处理器。 4. Controller 配置:配置控制器,即具体的请求处理器。 5. Interceptor 配置:配置拦截器,可以在请求处理之前或之后进行一些特定的操作。 以下是一个简单的 Spring MVC 的 XML 配置示例: ```xml <!-- 配置 DispatcherServlet --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置初始化参数 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 配置 DispatcherServlet 映射 URL --> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 配置处理器映射器 --> <bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> <!-- 配置控制器 --> <bean name="/hello" class="com.example.HelloController" /> <!-- 配置拦截器 --> <mvc:interceptors> <bean class="com.example.LogInterceptor" /> </mvc:interceptors> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值