zoukankan      html  css  js  c++  java
  • Spring MVC多动作控制器

    以下示例演示如何使用Spring Web MVC框架的多动作控制器。 MultiActionController类用于在单个控制器中分别映射多个URL到对应的方法。

    所下所示配置 -

    package com.yiibai.springmvc;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
    
    public class UserController extends MultiActionController{
    
       public ModelAndView home(HttpServletRequest request,
          HttpServletResponse response) throws Exception {
          ModelAndView model = new ModelAndView("home");
          model.addObject("message", "Home");
          return model;
       }
    
       public ModelAndView add(HttpServletRequest request,
          HttpServletResponse response) throws Exception {
          ModelAndView model = new ModelAndView("user");
          model.addObject("message", "Add");
          return model;
       }
    
       public ModelAndView remove(HttpServletRequest request,
          HttpServletResponse response) throws Exception {
          ModelAndView model = new ModelAndView("user");
          model.addObject("message", "Remove");
          return model;
       }
    }
    
    Java

    URL映射配置文件如下 -

    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
       <bean name="/home.html" class="com.yiibai.springmvc.UserController" /> 
       <bean name="/user/*.html" class="com.yiibai.springmvc.UserController" />
    
    XML

    例如,使用上面的配置,如果URI:

    • 对于/home.html请求,DispatcherServlet将请求转发到UserController类的 home()方法。
    • 对于user/add.htmlDispatcherServlet将请求转发到UserController类的 add()方法。
    • 对于user/remove.htmlDispatcherServlet将请求转发到UserController类的 remove()方法。

    首先,让我们使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序:

    1. 创建一个名称为 MultiActionController 的动态WEB项目。
    2. 在 com.yiibai.springmvc 包下创建一个Java类UserController
    3. jsp子文件夹下创建两个视图文件:home.jsp 和 user.jsp
    4. 最后一步是创建所有源和配置文件的内容并运行应用程序,详细如下所述。

    完整的项目文件目录结构如下所示 -

    UserController.java 的代码如下所示 -

    package com.yiibai.springmvc;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
    
    public class UserController extends MultiActionController{
    
       public ModelAndView home(HttpServletRequest request,
          HttpServletResponse response) throws Exception {
          ModelAndView model = new ModelAndView("home");
          model.addObject("message", "Home");
          return model;
       }
    
       public ModelAndView add(HttpServletRequest request,
          HttpServletResponse response) throws Exception {
          ModelAndView model = new ModelAndView("user");
          model.addObject("message", "Add");
          return model;
       }
    
       public ModelAndView remove(HttpServletRequest request,
          HttpServletResponse response) throws Exception {
          ModelAndView model = new ModelAndView("user");
          model.addObject("message", "Remove");
          return model;
       }
    }
    
    Java

    MultiActionController-servlet.xml 的代码如下所示 -

    <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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-3.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix" value="/WEB-INF/jsp/"/>
          <property name="suffix" value=".jsp"/>
       </bean>
    
       <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
       <bean name="/home.html" 
          class="com.yiibai.springmvc.UserController" /> 
       <bean name="/user/*.html" 
          class="com.yiibai.springmvc.UserController" />    
    </beans>
    
    XML

    home.jsp 的代码如下所示 -

    <%@ page contentType="text/html; charset=UTF-8" %>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Home</title>
    </head>
    <body>
    <body>
    <a href="user/add.html" >add()方法</a> <br>
    <a href="user/remove.html" >remove()方法</a>
    </body>
    </html>
    
    HTML

    user.jsp 的代码如下所示 -

    <%@ page contentType="text/html; charset=UTF-8" %>
    <html>
    <head>
    <title>User.jsp Page</title>
    </head>
    <body>
       <h2>${message}</h2>  
    </body>
    </html>
    
    HTML

    完成创建源和配置文件后,发布应用程序到Tomcat服务器。

    现在启动Tomcat服务器,现在尝试访问URL => http://localhost:8080/MultiActionController/home.html ,如果Spring Web应用程序没有问题,应该看到以下结果:

    当访问URL => http://localhost:8080/MultiActionController/add.html , 如果Spring Web应用程序没有问题,应该看到以下结果:

  • 相关阅读:
    ssh整合思想 Spring与Hibernate和Struts2的action整合 调用action添加数据库 使用HibernateTemplate的save(entity)方法 update delete get 等方法crud操作
    ssh整合思想 Spring与Hibernate和Struts2的action整合 调用action添加数据库 使用HibernateTemplate的save(entity)方法 ognl.MethodFailedException:Method"execute" failed for ssh包合集下载
    ssh整合思想 Spring与Hibernate的整合 项目在服务器启动则自动创建数据库表
    ssh整合思想 Spring与Hibernate的整合ssh整合相关JAR包下载 .MySQLDialect方言解决无法服务器启动自动update创建表问题
    将博客搬至CSDN
    Re-Order Oracle columns of tables
    在Word中的方框里打对勾
    Interpreting java.lang.NoSuchMethodError message
    TIMEZONE FOR UAE/BH/RU/PL
    申论写作五大标准“配件”
  • 原文地址:https://www.cnblogs.com/borter/p/9519637.html
Copyright © 2011-2022 走看看