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>

 

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

 

SpringMVC03controller中定义多个方法详解编程语言
SpringMVC03controller中定义多个方法详解编程语言

复制代码
<?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文件的配置

 

SpringMVC03controller中定义多个方法详解编程语言
SpringMVC03controller中定义多个方法详解编程语言

复制代码
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

 

SpringMVC03controller中定义多个方法详解编程语言
SpringMVC03controller中定义多个方法详解编程语言

复制代码
<?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文件中的配置

 

SpringMVC03controller中定义多个方法详解编程语言
SpringMVC03controller中定义多个方法详解编程语言

  <body> 
    ${result} 
  </body>

success.jsp页面内容

 

SpringMVC03controller中定义多个方法详解编程语言

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/12167.html

(0)
上一篇 2021年7月19日 14:26
下一篇 2021年7月19日 14:26

相关推荐

发表回复

登录后才能评论