zoukankan      html  css  js  c++  java
  • 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页面内容

  • 相关阅读:
    算法: 整数中1出现的次数(从1到n整数中1出现的次数)
    健身:肩部训练
    算法: 字符串的排列
    不能浮躁,还是需要沉淀;
    算法:从上往下打印二叉树
    健身:手臂训练
    抛出错误
    记录错误
    调用栈
    try/except/finally
  • 原文地址:https://www.cnblogs.com/999-/p/6117156.html
Copyright © 2011-2022 走看看