我们都知道,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. 网络openvpn各种问题

    今天先配置在同一个vmc下的一个openvpn里的两个虚拟机,同一个网段 一开始出现了如下问题: eth0: ERROR while getting interface flags: No such ...

  2. div 画园

    .destination1{ border: #666 solid 1px; box-shadow:-1px 1px 5px 0px #333; width:922px; height:485px; ...

  3. python中的map()函数

    MapReduce的设计灵感来自于函数式编程,这里不打算提MapReduce,就拿python中的map()函数来学习一下. 文档中的介绍在这里: map(function, iterable, .. ...

  4. fprintf宏

    最近在调试程序,使用printf函数和调试信息都不能在终端输出,所以使用比较笨的方法.将调试信息写到文件中,再查看文件.由于要多次使用fprintf函数,所以将其写成宏. 参考链接: http://w ...

  5. Python C :ctypes库

    >>> import ctypes >>> from ctypes import * >>> dir(ctypes) ['ARRAY', 'Arg ...

  6. Python3的tcp socket接收不定长数据包接收到的数据不全。

    Python Socket API参考出处:http://blog.csdn.net/xiangpingli/article/details/47706707 使用socket.recv(pack_l ...

  7. 转载:Create a Flash Login System Using PHP and MySQL

    本文共两部分: 1. http://dev.tutsplus.com/tutorials/create-a-flash-login-system-using-php-and-mysql-part-1- ...

  8. PHP大量数据循环时内存耗尽问题的解决方案

    最近在开发一个PHP程序时遇到了下面的错误:PHP Fatal error: Allowed memory size of 268 435 456 bytes exhausted错误信息显...分析: ...

  9. Java基础--生成验证码

    HTML <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnco ...

  10. Oracle-批量修改语句及相关知识点

    问: 有两张表A和B,结构相同,数据量一致,比如都有x,y和z列且都有n行,x为主键,完全相等,如何把表B的y列的数据赋值给A的y列? 我写的是1 update A set A.y=B.y where ...