我尝试将数据从jsp发送到控制器时出错404

时间:2015-02-13 15:50:21

标签: spring jsp java-ee

我有一个简单的J2ee应用程序。现在我想从我的jsp页面发送表单数据。

但是当我尝试点击按钮时,我的错误代码为404.我认为容器找不到资源。

所以这是我的jsp的代码:

<script language="javascript">  

            function saveRigaTable2(){

                try{
                    var ajaxHandler = new AjaxHandler('POST', '<c:out value="${pageName}"/>.htm', false);             
                    ajaxHandler.encodeParameter('method', '/saveItems');
                    ajaxHandler.encodeParameter('idRiga', 0);
                    ajaxHandler.callbackHandler = function()
                    {           
                        if (ajaxHandler.getRequest().readyState == 4)
                        {   
                            alert("pipo2 "+ajaxHandler.getRequest().status);                    
                            if (ajaxHandler.getRequest().status == 200)
                            {

                                var xmlDoc = ajaxHandler.getRequest().responseXML;
                                var items = xmlDoc.getElementsByTagName("exception");

                                if (items.length!=0)
                                {
                                    var punto=items[0];
                                    document.getElementById("error").innerHTML=punto.getAttribute("text");
                                    alert('Impossibile1 eseguire l\' operazione');
                                }
                                else
                                {
                                    reload();                           
                                }
                            }
                        }
                    };

                    ajaxHandler.startRequest();   
                }
                catch (e)
                {
                    alert('Impossibile eseguire l\' operazione');
                }      
            }
     </script>

<input type="button" value="Inserisci" onclick="javascript:saveRigaTable2();"/>

这是RegistrazioneController.java的代码

package com.springmvcapp.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

@Controller
@RequestMapping("/registrazione")
public class RegistrazioneController extends MultiActionController{

    @RequestMapping(value = "/registrazione")
    public ModelAndView registrazione(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView model = new ModelAndView("registrazione");
        model.addObject("msg", "hello world");
        model.addObject("pageName", getPageName());
        return model;
    }


    @RequestMapping(value = "/registrazione/sendDati")
    public String getDataFromForum(@RequestParam String nome){
        System.out.println("pippo");
        return nome;
    }
    @RequestMapping(value = "/saveItems")
    public ModelAndView saveItems(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        List dataSet = new ArrayList();
        Map dataItem = new HashMap();
        List attributes = new ArrayList();   
        Map model=new HashMap();
        try
        {
            request.getSession().setAttribute("rilasciataModificataConf", "SI");
        } catch (Exception e) {
            dataItem.put("tagName", "exception");
            //attributes.add(new Entity("text", "Error in saving the current row" ));
            dataItem.put("attributes", attributes);
            dataSet.add(dataItem);  
            model.put("dataSet", dataSet);
            //MbuLoggerFactory.logStackTrace(e);
        }

        return loadPage(request, response);
    }

    public ModelAndView loadPage(HttpServletRequest request,HttpServletResponse response) throws Exception
    {
        Map model=null;
        try {
            request.getSession().removeAttribute("sessioneBu");
            request.getSession().removeAttribute("currentRichiesta");

            model = (Map)request.getAttribute("model");     
            if (model==null) model=new HashMap();


            model.put("pageName", getPageName());
            model.put("pageTitle", "richiestaConferimenti");


        } catch (Exception e) {
            throw e;
        }

        return internalLoadPage(request,response,model);
    }

    public ModelAndView internalLoadPage(HttpServletRequest request, HttpServletResponse response, Map model) throws Exception 
    {
        return new ModelAndView(getViewName(), model);  
    }

    public String getViewName() throws Exception {
        return "registrazione";
    }

    public String getPageName() throws Exception {
        return "registrazione";
    }
}

这是springmvcapp-servlet.xml

的配置
<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.springmvcapp.controller" />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

这是web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
  <display-name>SpringMVCApp</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>springmvcapp</servlet-name>
    <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvcapp</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>
</web-app>

错误在哪里?

1 个答案:

答案 0 :(得分:0)

${pageName}的价值是什么?

您可以尝试使用<c:url>代替<c:out>

var ajaxHandler = new AjaxHandler('POST', '<c:out value="${pageName}"/>

您还定义了控制器级别RequestMapping,并且在每个方法上都会有重复的URI。