我们都知道,servlet代码一般来说只能在一个servlet中做判断去实现一个servlet响应多个请求,

但是springMVC的话还是比较方便的,主要有两种方式去实现一个controller里能响应多个请求

第一种:继承MultiActionController类,这种方法已经废弃了,但是也能用

项目结构

具体配置

<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" > <context:component-scan base-package="controller" />
<bean id="paramMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="cmd"></property> <!-- value可以改成action,访问 的时候也要改成action=方法名 -->
</bean>
<bean name="/pay" class="controller.MyController">
<property name="methodNameResolver">
<ref bean="paramMethodResolver"/>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"><value>/WEB-INF/pages</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>
</beans>

 

web.xml没改 

<web-app>
  
    <display-name>Spring MVC Application</display-name>
  
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
  
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
  
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:mvc-dispatcher-servlet.xml</param-value>
    </context-param>
  
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
  
</web-app>

hello.jsp 

<%@ 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>
This is my JSP page. <br>
本次调用方法是<b>${message }</b>
</body>
</html>

控制器

package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONObject; import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController; @SuppressWarnings("deprecation")
@Controller
public class MyController extends MultiActionController {
public ModelAndView add(HttpServletRequest request,
HttpServletResponse response) {
System.out.println("----add----");
return new ModelAndView("/hello", "message", "add");
}
public ModelAndView del(HttpServletRequest request,
HttpServletResponse response) {
System.out.println("----del----");
return new ModelAndView("/hello", "message", "del");
} //接收单个字符串
public void update(HttpServletRequest request,
HttpServletResponse response){
String name=request.getParameter("name");
System.out.println("name:"+name);
}
}

  

访问方式 

http://localhost:8080/MyWeb/pay?cmd=update&name=xxxx

后台输出了

name:xxxx

输入

http://localhost:8080/MyWeb/pay?cmd=add

前台显示为

This is my JSP page.
本次调用方法是add

  

第二种:就是最常用的用注解了  

spring配置

<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" >
<!-- 自动扫描,根据注解注入 -->
<context:component-scan base-package="controller" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"><value>/WEB-INF/pages</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>
</beans>

  

控制器 

package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping("/pay")
public class MyController{
@RequestMapping("/add")
public ModelAndView add(HttpServletRequest request,
HttpServletResponse response) {
System.out.println("----add----");
String name=request.getParameter("name");
System.out.println(name);
return new ModelAndView("/hello", "message", "add");
}
@RequestMapping("/del")
public ModelAndView del(HttpServletRequest request,
HttpServletResponse response) {
System.out.println("----del----");
String name=request.getParameter("name");
System.out.println(name);
return new ModelAndView("/hello", "message", "del");
} }

  

调用方式

http://localhost:8080/MyWeb1/pay/add

前台结果

This is my JSP page.
本次调用方法是add

  

  

 

SpringMVC实现简单应用的更多相关文章

  1. SpringMVC之简单的增删改查示例(SSM整合)

    本篇文章主要介绍了SpringMVC之简单的增删改查示例(SSM整合),这个例子是基于SpringMVC+Spring+Mybatis实现的.有兴趣的可以了解一下. 虽然已经在做关于SpringMVC ...

  2. spring+springMVC+mybatis简单整合

    spring+springMVC+mybatis简单整合, springMVC框架是spring的子项目,所以框架的整合方式为,spring+Mybatis或springMVC+mybatis. 三大 ...

  3. springmvc springJDBC 简单实训银行账户管理系统

    springmvc springJDBC 简单实训银行账户管理系统 1.简单介绍一下,在校时每周结束都会有一次学习总结,简称“实训”,这次实训内容是spring,因为是最近热门框架,我就先从基础方面开 ...

  4. Maven+SpringMVC+Dubbo 简单的入门demo配置

    转载自:https://cloud.tencent.com/developer/article/1010636 之前一直听说dubbo,是一个很厉害的分布式服务框架,而且巴巴将其开源,这对于咱们广大程 ...

  5. Spring+SpringMVC+Hibernate简单整合(转)

    SpringMVC又一个漂亮的web框架,他与Struts2并驾齐驱,Struts出世早而占据了一定优势,下面同样做一个简单的应用实例,介绍SpringMVC的基本用法,接下来的博客也将梳理一下Str ...

  6. SpringMvc的简单介绍

    1.mcv框架要做哪些事情 (a)将url映射到java类或者Java类的方法 (b)封装用户提交的数据 (c)处理请求---调用相关的业务处理,封装响应的数据 (d)将封装的数据进行渲染,jsp,h ...

  7. java之Maven配置和springMvc的简单应用

    初始springMvc这个框架,非常的陌生,而且幸好公司是通过maven这个代码管理工具,可以随时添加依赖.解决了很多问题在以后深入开发中. 项目结构: 通过结构中,pom.xml这个文件其实就说明这 ...

  8. SpringMVC学习简单HelloWorld实例

    首先还是从一个简单的Hello World项目说起: 我机器的开发环境为: Ubuntu12.04(不同操作系统对本系列项目没有影响): 开发工具:Eclipse For JavaEE: 数据库:My ...

  9. 关于springmvc接受简单参数和List集合数据的实现

    首先要创建一个搭建一个springmvc的工程,至于如何搭建这里就不说了.给出比较重要的配置,项目目录结构如下,弄的比较简单,因为最近遇到一个需要传递List集合数据的问题,所以就当做实验. web. ...

  10. springmvc+quartz简单实现定时调度

    一.简介:Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简单或为运行十 ...

随机推荐

  1. PHP注释的艺术——phpDoc规范

    用过IDE或看过其他源码的小伙伴们应该都见过类似下面这样的注释   /** * 递归获取所有游戏分类 * @param int $id * @return array */ 看得多了就大概知道了一些规 ...

  2. 12款优秀 jQuery Ajax 分页插件和教程

    12款优秀 jQuery Ajax 分页插件和教程 在这篇文章中,我为大家收集了12个基于 jQuery 框架的 Ajax 分页插件,这些插件都提供了详细的使用教程和演示.Ajax 技术的出现使得 W ...

  3. 《FPGA全程进阶---实战演练》第三章之PCB设计之过孔

    在画电路板时,往往需要过孔来切换层之间的信号.在PCB设计时,过孔的选择有盲孔,埋孔,通孔.如图3.1所示.盲孔是在表面或者底面打通到内层面,但不打穿,埋孔是在内层面之间的孔,不在表面和底面漏出:通孔 ...

  4. 关于在Andoird集成开发软件中添加外部jar包的方法

    步骤必须是下面的两步,少一步都不行. 第一步是存放于项目中,第二步是导入和应用于项目中. 1.右键项目-Build Path-Configure Build Path-在Libraries目录下-点右 ...

  5. 【转】【OPenGL】OPenGL 画图板-- 中点算法画圆

    为了能以任意点为圆心画圆,我们可以把圆心先设为视点(相当于于将其平移到坐标原点),然后通过中点法扫描转换后,再恢复原来的视点(相当于将圆心平移回原来的位置). 圆心位于原点的圆有四条对称轴x=0,y= ...

  6. Android ArryaList 笔记

    Arraylist相当于动态数组,可以动态的添加或者删除其中的元素. 参考链接 http://beginnersbook.com/2013/12/java-arraylist/ package com ...

  7. bootstrap -- css -- 表单控件

    若干css样式 .form-control { display: block; width: 100%; height: 34px; padding: 6px 12px; font-size: 14p ...

  8. 转载:【原译】Erlang常见注意事项(Efficiency Guide)

    转自:http://www.cnblogs.com/futuredo/archive/2012/10/17/2726416.html Common Caveats(常见注意事项) Erlang/OTP ...

  9. 真想用c#开发个 wp五笔输入法。。。奈何网上资料太少,源码都是c++写的。求大神指点!!!

    真想用c#开发个 wp五笔输入法...奈何网上资料太少,源码都是c++写的.求大神指点!!!!

  10. 讨论CSS中的各类居中方式

    今天主要谈一谈CSS中的各种居中的办法. 首先是水平居中,最简单的办法当然就是 margin:0 auto; 也就是将margin-left和margin-right属性设置为auto,从而达到水平居 ...