欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-03-05 11:24:53
...
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>

相关标签: spring mvc java

上一篇: hql占位符

下一篇: maven知识