SpringMVC03controller中定义多个方法

<%@ 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>

     <%-- 这里的参数要和 配置文件中ParameterMethodNameResolver的paramName属性的value值相对应--%>

    <a href="hello?action=add">新增</a>
    <a href="hello?action=update">修改</a>
    <a href="hello?action=del">删除</a>
    <a href="hello?action=find">查询</a>
  </body>
</html>

webroot下面的index.jsp页面

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!-- ParameterMethodNameResolver:会根据前台请求的参数名称 对应到指定的controller中  方法名称
  前台的请求中 必须携带参数  action=xxx    如果需要自己定义  则需要更改paramName的value值
  -->
  <bean id="parameter"  class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
    <property name="paramName"  value="action"/><!-- 可以省略 因为是默认值 -->
  </bean>

    <!-- 处理器 -->
    <bean name="/hello" class="cn.bdqn.controller.MyController">
     <property name="methodNameResolver" ref="parameter"/>
    </bean>

    <!-- 配置视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

springmvc-servlet.xml文件的配置

public class MyController extends MultiActionController {

    // 新增
    public ModelAndView add(HttpServletRequest request,
            HttpServletResponse response) {
        return new ModelAndView("success", "result", "新增成功界面");
    }

    // 修改
    public ModelAndView update(HttpServletRequest request,
            HttpServletResponse response) {
        return new ModelAndView("success", "result", "修改成功界面");
    }

    // 删除
    public ModelAndView del(HttpServletRequest request,
            HttpServletResponse response) {
        return new ModelAndView("success", "result", "删除成功界面");
    }

    // 查看
    public ModelAndView find(HttpServletRequest request,
            HttpServletResponse response) {
        return new ModelAndView("success", "result", "查看成功界面");
    }

}

对应的controller

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <!--配置springmvc的核心控制器  -->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--  配置spring配置文件的位置 以及名称 -->
    <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:/springmvc-servlet.xml</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

web.xm文件中的配置

  <body>
    ${result}
  </body>

success.jsp页面内容

时间: 2024-08-28 11:21:50

SpringMVC03controller中定义多个方法的相关文章

Vue-Router路由Vue-CLI脚手架和模块化开发 之 在单文件组件项目中定义数据、方法和组件之间的相互引用

定义数据 根据上一篇博文配置项目的结构的基础上继续进行优化: 在app.vue中的导出模块/组件部分设置其属性: export default{//导出模块/组件 data(){ return{ name:'perfect', count:0 } }, 在一个template标签中进行调用: <template> <div> <h2> 欢迎来到perfect*的博客园!!!</h2> <h3>{{name}}</h3> </te

【enum】如何在枚举中定义自定义的方法,并进行使用

1.定一个枚举类 package com.eud.t1; public enum Color { //定义枚举中的常量 RED(1,"hongse"), GREEN(2,"qingse"),BLACK(3,"heise"); private int code; private String name; public int getCode() { return code; } public void setCode(int code) { thi

JS中定义类的方法&lt;转&gt;

转载地址:http://blog.csdn.net/sdlfx/article/details/1842218 PS(个人理解): 1) 类通过prototype定义的成员(方法或属性),是每个类对象共有的,一般不用来定义成员属性,一个对象修改了属性值,所有对象均被修改: 2) 类拥有prototype属性,类对象没有: 3) 每次new类对象或直接调用类(以下工厂方法形式),都会把定义类(function)的语句执行一次(单例模式可以避免这个情况): 4) 类是function类型,类对象是o

SpringMVC04controller中定义多个方法

public class MyController extends MultiActionController { // 新增 方法修饰符要是public public ModelAndView add(HttpServletRequest request, HttpServletResponse response) throws Exception { return new ModelAndView("/WEB-INF/jsp/success.jsp", "result&q

JS中定义类的方法

S中定义类的方式有很多种: 1.工厂方式 function Car(){    var ocar = new Object;    ocar.color = "blue";    ocar.doors = 4;    ocar.showColor = function(){     document.write(this.color)    };    return ocar;   }   var car1 = Car();   var car2 = Car(); 调用此函数时将创建新

C++类中定义常量的方法

好久没用过C++,本来就不太熟悉,今天突然写到一个类,需要在类中定义一个常量,居然花了很长时间. 刚开始写了static const int num = 100: 这样是不行的,因为常量只能在初始化列表中初始化,如果去掉const的话,又不能有"常量"的效果, 在类外用宏定义看起来不科学,类外const也一样,后面就大概搜索了一下,可以选择用枚举类型来替代达到 差不多的效果.具体的代码看下面: class User{ public: enum { MaxNum = 20}; } 写下来

Js中的假值_ES5中定义的ToBoolean方法强制类型转换后值为false

你不知道的Javascript(中)--ToBoolean javascript中的值可以分为以下两类: 1.可以被强制类型转换为false的值 2.其他(被强制类型转换为true的值) 假值---以下是js的ES规范中定义了的假值: undefined null false +0.-0和NaN ""-空字符串 假值的不二强制类型转换结果为false.一般除假值以外的都是真值. 假值对象--封装了假值的对象 eg: var a=new Boolean(false); var b=new

php中定义数组的方法

1.PHP定义数组的格式 数组名=array(); 如:$aa=array();//这样就定义了一个数组, 之后给元素赋值: $aa[0]="9016"; $aa[1]="9017"; $aa[2]="9018"; 2.PHP输出数组的方法: foreach($aa as $val) { echo$val; } 也可以在定义数组时直接赋值 $aa=array(0=>"9016",1=>"9017"

自定义类中定义两个方法,相互调用

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Test06 7 { 8 //class Class1 9 //{ 10 // public string Country() 11 // { 12 // string strCountry = "方法的示例!"; 13 // return strCountry; 14 /