前言:我在Tomcat或Tomee服务器上没有这个问题。
我试图通过ajax执行一个简单的Get请求,当请求到达服务器时,它会执行错误的方法。
JS档案
function CustomerPoints() {
this.initializeListeners = function () {
$("#storeSelect").change(function (event) {
event.preventDefault();
customerPoints.callAjax(contextPath + "/loyaltyPoints/customerPoints?getPoints&id=" + $("#storeSelect").val());
});
this.callAjax = function (url, value) {
// generated url = http://localhost:9080/TLC/loyaltyPoints/customerPoints?getPoints&id=3778
var cardNumber = $("#customerNumber");
var alternateId = $("#alternateId");
$
.ajax(
url,
{
type: "GET",
data: value,
beforeSend: function (req) {
$("#modalErrorDiv").empty();
req.setRequestHeader("Accept",
"text/html;type=ajax");
req.setRequestHeader($(
"meta[name='_csrf_header']").attr(
"content"), $("meta[name='_csrf']")
.attr("content"));
},
complete: function (jqXHR) {
$("#customerNumber").replaceWith(cardNumber);
$("#alternateId").replaceWith(alternateId);
customerPoints.initializeListeners();
},
success: function (data, textStatus, jqXHR) {
$("#content").replaceWith(data);
},
error: function (jqXHR, textStatus, errorThrown) {
$('html,body').animate({
scrollTop: $('body').offset().top
}, 'slow');
$("#modalErrorDiv").empty();
$("#modalErrorDiv")
.append("<ul ><li class='error'>An error has occured. Please contact support.</li></ul>");
}
});
};
控制器
@Controller
public class CustomerPointsController
{
private static final String HTML_PAGE = "loyaltyPoints/customerPoints";
private static final String REQUESTMAPPING = "/" + HTML_PAGE;
private static final String APPLYPOINTSMAPPING = "loyaltyPoints/applyPoints :: #content";
private static final String CONTENT = HTML_PAGE + " :: #content";
/**
* @param model
* @return
*/
@RequestMapping(value = REQUESTMAPPING)
public String maintainCustomerPoints(Model model)
{
// this is the method that is called everytime
return HTML_PAGE;
}
/**
* @param id
* @param model
* @returns the valid customer points for this store, default followed by promotion points
* when a user picks a store from the drop down.
*/
@RequestMapping(value = REQUESTMAPPING, method = RequestMethod.GET, params = {
"getPoints"})
public String getPoints(@RequestParam(value = "id") String id, Model model)
{
// this is the method that never gets called
return CONTENT;
}
}
注意我还从页面脚本中获取了生成的URL并粘贴到我的浏览器中以执行。它只执行maintainCustomerPoints
。
server.xml
<?xml version="1.0" encoding="UTF-8"?>
<server description="tlc server">
<!-- Enable features -->
<featureManager>
<feature>jsf-2.0</feature>
<feature>jsp-2.2</feature>
<feature>ssl-1.0</feature>
<feature>localConnector-1.0</feature>
<feature>restConnector-1.0</feature>
<feature>json-1.0</feature>
<feature>jaxrs-1.1</feature>
<feature>servlet-3.0</feature>
<feature>jpa-2.0</feature>
<feature>beanValidation-1.0</feature>
<feature>jndi-1.0</feature>
<feature>jdbc-4.0</feature>
<feature>monitor-1.0</feature>
</featureManager>
<httpEndpoint id="defaultHttpEndpoint" host="*" httpPort="9080" httpsPort="9443" virtualHost="default_host" />
<jdbcDriver id="DerbyJDBCDriver">
<library name="DerbyLib">
<fileset dir="C:\tools\servers\wlp\lib" includes="derbyclient-10.6.1.0.jar" />
</library>
</jdbcDriver>
<dataSource jndiName="jdbc/TLCDataSource" id="derbyDataSource" jdbcDriverRef="DerbyJDBCDriver">
<properties.derby.client databaseName="TLC" password="APP" user="APP" />
</dataSource>
<applicationMonitor updateTrigger="mbean" />
<application id="TLC_war" context-root="/TLC" location="C:\Users\nd26434\gitrepos\tlc\target\TLC-1.0.0-SNAPSHOT.war" name="TLC_war" type="war">
<classloader delegation="parentLast">
<privateLibrary>
<fileset dir="C:\tools\servers\wlp\lib" includes="aspectjweaver-1.8.0.jar" />
</privateLibrary>
</classloader>
</application>
</server>
答案 0 :(得分:1)
根据documentation @RequestMapping
,如果params
形式中有{"myParam"}
,则假定此参数必须存在于请求中且可能具有任何值。< / p>
因此,网址的安全形式应包含getPoints
的任何值,例如customerPoints?getPoints=anyValueHere
。
当没有值存在时,request.getParameter()
方法可能会为Tomcat返回不同的值。对于Liberty,如果null
没有=
,则会customerPoints?getPoints
,non null
和=
customerPoints?getPoints=
1>