您的位置:首页 > 编程语言 > Java开发

springMVC之多方法访问

2016-01-31 10:39 501 查看
这个和Struts2是一样的,往往我们在编写程序的时候,会涉及到很多的方法,很多的模块,不可能我们每一个方法都要为其新建一个controller吧,所以这个时候我们往往会将一个模块的相关方法放到一起,简化了开发,也方便了管理。那么在springMVC中该如何进行多方法的访问呢,请往下看:

1、配置环境搭建(略...)

2、编写springMVC-xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
       xmlns:mvc="http://www.springframework.org/schema/mvc"  
       xmlns:context="http://www.springframework.org/schema/context"  
       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.1.xsd  
            http://www.springframework.org/schema/context   
            http://www.springframework.org/schema/context/spring-context-3.1.xsd  
            http://www.springframework.org/schema/mvc  
            http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> 
    
    <bean id="/test1/multi" class="com.tgb.web.controller.MultiController">
       
<!-- bean引用方法解析器 -->

        <property name="methodNameResolver">
            <ref bean="paramMethodResolver" />
        </property>
    </bean>
    
    <!-- 配置方法解析器 -->
    <bean id="paramMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver" >
        <property name="paramName" value="action"></property>
    </bean>
    
    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/> 
        <property name="suffix" value=".jsp"/> 
    </bean>
 </beans>  

3、编写controller

package com.tgb.web.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class MultiController extends MultiActionController {
    public ModelAndView add(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception{
        System.out.println("----------add()--------------");
        return new ModelAndView("/multi","method","add");
    }
    
    public ModelAndView update(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception{
        System.out.println("----------update()--------------");
        return new ModelAndView("/multi","method","update");
    }
}

4、编写返回页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <h2>当前调用方法:${method }</h2>
</body>
</html> 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  SSH spring mvc