[转]spring mvc 多方法-简单例子_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > [转]spring mvc 多方法-简单例子

[转]spring mvc 多方法-简单例子

 2013/8/6 13:08:21  zh_harry  程序员俱乐部  我要评论(0)
  • 摘要:packagecom.jp.action;importjava.util.HashMap;importjava.util.Map;importjava.util.logging.Logger;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.springframework.web.bind.RequestUtils;importorg
  • 标签:MVC 方法 例子 Spring
package com.jp.action;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.RequestUtils;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.InternalResourceView;

import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
/**


     //    实现Controller接口中的handleRequest方法
    public ModelAndView handleRequest(HttpServletRequest req,
            HttpServletResponse res) throws Exception {
        // TODO Auto-generated method stub
       
        Map model=new HashMap();
        model.put("helloWorld",getHelloWorld());
        return new ModelAndView(getViewPage(),model);
       
    }

* @author user
*
*/

//http://localhost:8080/myApp/helloWorld.do


//实现了Controller接口,需要在导入spring.jar包
public class HelloWorldAction extends MultiActionController{
    private Logger logger=Logger.getLogger(this.getClass().getName());
   // private String helloWorld;
    private String viewPage;

   

    public String getViewPage() {
        return viewPage;
    }

    public void setViewPage(String viewPage) {
        this.viewPage = viewPage;
    }
   
    public ModelAndView insert(HttpServletRequest req,
            HttpServletResponse res) throws ServletRequestBindingException {
        // TODO Auto-generated method stub
        String helloWorld=RequestUtils.getRequiredStringParameter(req, "helloWorld");
        Map model=new HashMap();
        model.put("helloWorld","insert:"+helloWorld);
        return new ModelAndView(getViewPage(),model);
    }
   
    public ModelAndView delete(HttpServletRequest req,
            HttpServletResponse res) throws ServletRequestBindingException {
        // TODO Auto-generated method stub
        String helloWorld=RequestUtils.getRequiredStringParameter(req, "helloWorld");
        Map model=new HashMap();
        model.put("helloWorld","delete:"+helloWorld);
        return new ModelAndView(getViewPage(),model);
    }
   
    public ModelAndView update(HttpServletRequest req,
            HttpServletResponse res) throws ServletRequestBindingException {
        // TODO Auto-generated method stub
        String helloWorld=RequestUtils.getRequiredStringParameter(req, "helloWorld");
        Map model=new HashMap();
        model.put("helloWorld","update:"+helloWorld);
        return new ModelAndView(getViewPage(),model);
    }
   


   

}



---dispatcherServlet-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <!-- 定义映射 -->
    <bean id="urlMapping"
        class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="helloWorld.do">helloWorldAction</prop>
            </props>
        </property>
    </bean>
    <!-- 定义视图 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass">
            <value>org.springframework.web.servlet.view.InternalResourceView</value>
        </property>
        <!-- 定义JSP存放路径 -->
         <property name="prefix">
            <value>/WEB-INF/jsp/</value>
         </property>
         <!-- 定义JSP页面后缀 -->
         <property name="suffix">
            <value>.jsp</value>
         </property>
        
    </bean>

    <!-- 定义控制器 -->
    <bean id="helloWorldAction" class="com.jp.action.HelloWorldAction">
        <property name="methodNameResolver">
            <ref bean="paraMethodResolver"/>
        </property>
        <property name="viewPage">
            <value>show</value>
        </property>
    </bean>
   
    <!-- 定义多动作参数 -->
    <bean id="paraMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
        <property name="paramName">
            <value>method</value>
        </property>
    </bean>

</beans>

-----JSP

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head><title>第二个SpringMVC实例</title></head>

<body>

<form name="HelloWorld" action="/springmvc/helloWorld.do" method="post">
    欢迎语:<input type="text" name="helloWorld" value=""/><br/>
<input type="submit" name="method" value="提交">
<input type="submit" name="method" value="insert">
<input type="submit" name="method" value="update">
<input type="submit" name="method" value="detele">
<input type="submit" name="method" value="select">
</form>

</body>
</html>



---SHOW.JSP

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head><title>第二个SpringMVC实例</title></head>
<%String str=(String)request.getAttribute("helloWorld"); %>
<body>
<H1><%=str %></H1>
</body>
</html>

发表评论
用户名: 匿名