• SpringMVC实现简单应用


     我们都知道,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
    

      

      

     

  • 相关阅读:
    关于JDK中的设计模式
    关于Java中的构造方法和set方法()给属性赋值
    关于Object[]数组强转成Integer[]类型的数组.
    [ImportNew]Java中的Timer类和TimerTask类
    关于Linux最基本总结
    关于数组
    关于Linux系统和Windows系统中文件夹的命名规范
    关于数组集合之间的转换
    关于Java获取系统信息
    关于Windows常用命令
  • 原文地址:https://www.cnblogs.com/JAYIT/p/5591549.html
Copyright © 2020-2023  润新知