Salve a tutti,
sto iniziando a sviluppare un progetto utilizzando J2EE e spring.
Di seguito il file web.xml
codice:
<?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>
codice springmvcapp-servlet.xml
codice:
<?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>
Attraverso la Index.jsp, accedo ad una pagina registrazione.jsp con questo codice
codice:
<form action="registrazione.html">
<button class="btn btn-primary btn-lg" data-toggle="modal" onclick="submit">
Registrazione
</button>
</form>
Di seguito il codice della jsp registrazione.jsp:
codice:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="utf-8">
<title>Registrazione</title>
<meta name="description" content="">
<meta name="author" content="">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/font-awesome.css">
<link rel="stylesheet" type="text/css" href="css/animate.css">
<link rel="stylesheet" type="text/css" href="css/style.css">
<link href='http://fonts.googleapis.com/css?family=Montserrat' rel='stylesheet' type='text/css'>
<script type="text/javascript" src="js/jquery-2.1.0.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/ajaxHandler.js"></script>
<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>
</head>
<body>
<input type="button" value="Inserisci" onclick="javascript:saveRigaTable2();"/>
</body>
</html>
Ma quando clicco sul pulsante Inserisci, ho errore 404. Quindi credo che non riesce a trovare la pagina. Di seguito il controller, mi dite dove sbaglio per favore?
codice:
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 = "/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";
}
}