在我正在开发的网站上,我正在使用大量的Ajax调用来显示信息。
Theses Ajax Call如下:
function deleteBookingAjax(rowId) {
$.ajax({
url : "deleteRent.htm",
type : "GET",
data : {
"rentId" : rowId
},
dataType : 'json',
cache : false,
success : function(response) {
if(response.error) {
showPopupMessage(response.error, true);
}
},
statusCode : {
500 : function() {
loggingMessage('Error 500');
reloadBookingTable();
},
404 : function() {
loggingMessage('Error 404');
reloadBookingTable();
}
}
});
}
要执行此调用,我还有以下控制器:
@RequestMapping(value = "/deleteRent.htm")
public String deleteRent(Long rentId, HttpServletRequest request, HttpServletResponse response) {
if (rentId == null) {
return null;
}
try {
rentService.deleteRent(rentId);
} catch (Exception e) {
LOGGER.error(e);
}
response.setStatus(HttpStatus.SUCCESS);
return ViewNames.BOOKINGS_PAGE;
}
但我的问题是:要执行此Ajax调用,我需要创建一个无用的JSP文件: WEB-INF / jsp / deleteRent.jsp
<%@page import="net.****.****.web.controllers.RentControllers"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
如果我不创建这些文件,则Ajax调用无效...
当文件不在WEB-INF / Ajax调用中时返回:
GET http://127.0.0.1/deleteRent.htm?rowId=1 404 (Not Found)
日志显示:
2015-07-31 10:47:39,084 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Could not complete request
javax.servlet.ServletException: File "/WEB-INF/jsp/deleteRent.jsp" not found
at org.apache.jasper.servlet.JspServlet.handleMissingResource(JspServlet.java:417)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:384)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
....
如何在没有这些文件的情况下使其工作(我假设它将在配置中......但究竟在哪里以及如何?),因为拥有大量JSP文件并不容易,但只有少数文件具有内容...
编辑
这是我的context.xml的内容,如果errror位于那里,我们永远不会知道:
<!-- resources exclusions from servlet mapping -->
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/scripts/**" location="/scripts/" />
<mvc:resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**" />
<!-- Enables the Spring MVC @Controller programming model -->
<context:component-scan base-package="net.****.****.web" />
<context:annotation-config />
<mvc:annotation-driven />
<bean id="properties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:/META-INF/messages_en.properties</value>
</list>
</property>
</bean>
<bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/jsp directory -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
<property name="alwaysInclude" value="true" />
<property name="contentType" value="text/html; charset=UTF-8" />
</bean>
<bean id="annotationMethodHandlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
提前致谢。 :)
答案 0 :(得分:1)
我不确定你是否在寻找这个,但你可以在下面包含你的applicationContext.xml或你的***** - servlet.xml文件。
<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>
将此添加到您的pom中。现在您可以从方法中返回String。
<!-- Jackson JSON Mapper -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.10</version>
</dependency>
答案 1 :(得分:0)
答案是愚蠢的:)但至少现在已经修复了:
将void
放到函数中修复了问题。
我删除了空文件并且没有工作:)。
@RequestMapping(value = "/deleteRent.htm")
public void deleteRent(Long rentId, HttpServletRequest request, HttpServletResponse response) {
if (rentId == null) {
return null;
}
try {
rentService.deleteRent(rentId);
} catch (Exception e) {
LOGGER.error(e);
}
response.setStatus(HttpStatus.SUCCESS);
}