使用 Spring 3 和 Tiles 2 对 Web 应用程序进行 Ajax REST 调用
我有一个使用 spring 3 和tiles 2(不是 RESTful)构建的网络应用程序。我正在使用 UrlBasedViewResolver 显示视图
我的 application-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<context:component-scan base-package="com.xxx.xxxx.xxxxx.web.controller" />
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
</beans>
我的tiles.xml
<tiles-definitions>
<definition name="base.definition" template="/WEB-INF/views/layout.jsp">
<put-attribute name="header" value="/WEB-INF/views/header.jsp" />
<put-attribute name="body" value="" />
<put-attribute name="footer" value="/WEB-INF/views/footer.jsp" />
</definition>
<definition name="welcome" extends="base.definition">
<put-attribute name="body" value="/WEB-INF/views/welcome.jsp" />
</definition>
</tiles-definition>
现在我的问题是 - 我在客户端有一个动态表单,它是根据用户选项(例如,如果选择了某个选项)动态生成的(使用 javascript)从第一个下拉框开始,然后加载表格的其余部分,依此类推。对于我的第一个下拉菜单,我没有任何问题,因为这些选项是常量。但对于我的第二个下拉菜单选择,我需要加载特定且仅在服务器上可用的信息,所以我想到进行 AJAX 调用(RESTful 调用),所以我创建了一个像这样的控制器
我的 RESTful 控制器:
@Controller
public class UtilsController {
private WebAdministration admin;
public UtilsController() throws WebAdministrationException {
this.admin = WebAdministration.getInstance();
}
@RequestMapping(value="/aaaa/${site}/${type}", method=RequestMethod.GET, headers="Accept=text/xml, application/json")
public @ResponseBody List<String> getRemoteSites(@PathVariable("site") String site, @PathVariable("type") String type) {
List<String> remoteSites = null;
Config config = (Config) admin.getImplemantationData().get(Implementations.IMPL);
if (type.equals(Config.PERSISTENCE)) {
remoteSites = config.getRemoteSites().get(site).getNewRemoteSites();
}
else if (type.equals(Config.OLD)) {
remoteSites = config.getRemoteSites().get(site).getOldRemoteSites();
}
return remoteSites;
}
}
我的 ajax 调用:
<script type="text/javascript">
function getRemoteSites(site, type) {
$.getJSON('/aaaa/'+site+'/'+type, {
ajax : 'true'
}, function(data) {
var len = data.length;
for ( var i = 0; i < len; i++) {
document.write('<form:checkbox path="remoteSites" value="' + valueArray[i] + '" />');
}
});
};
</script>
我的 Ajax 请求失败并显示 404 - 未找到映射。
我怎样才能让我的 REST url 工作。我知道我需要一个新的视图解析器,但在过去的两天里我尝试了很多事情并寻找正确的解决方案。
有什么帮助吗?
谢谢
I have a webapp which is built using spring 3 and tiles 2 (not RESTful). I am using UrlBasedViewResolver to display views
my application-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<context:component-scan base-package="com.xxx.xxxx.xxxxx.web.controller" />
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
</beans>
my tiles.xml
<tiles-definitions>
<definition name="base.definition" template="/WEB-INF/views/layout.jsp">
<put-attribute name="header" value="/WEB-INF/views/header.jsp" />
<put-attribute name="body" value="" />
<put-attribute name="footer" value="/WEB-INF/views/footer.jsp" />
</definition>
<definition name="welcome" extends="base.definition">
<put-attribute name="body" value="/WEB-INF/views/welcome.jsp" />
</definition>
</tiles-definition>
Now my problem is - I have a dynamic form on client side that is generated on fly (using javascript) based on users options like, if a option is chosen from 1st drop down box, then I load rest of the form and so on. For my first drop down menu I have no issues, because those options are constants. but for my second drop down menu choice I need to load info that is specific and is only available on server, so I thought of making a AJAX call (RESTful call), so I created a Controller like this
my RESTful controller:
@Controller
public class UtilsController {
private WebAdministration admin;
public UtilsController() throws WebAdministrationException {
this.admin = WebAdministration.getInstance();
}
@RequestMapping(value="/aaaa/${site}/${type}", method=RequestMethod.GET, headers="Accept=text/xml, application/json")
public @ResponseBody List<String> getRemoteSites(@PathVariable("site") String site, @PathVariable("type") String type) {
List<String> remoteSites = null;
Config config = (Config) admin.getImplemantationData().get(Implementations.IMPL);
if (type.equals(Config.PERSISTENCE)) {
remoteSites = config.getRemoteSites().get(site).getNewRemoteSites();
}
else if (type.equals(Config.OLD)) {
remoteSites = config.getRemoteSites().get(site).getOldRemoteSites();
}
return remoteSites;
}
}
my ajax call:
<script type="text/javascript">
function getRemoteSites(site, type) {
$.getJSON('/aaaa/'+site+'/'+type, {
ajax : 'true'
}, function(data) {
var len = data.length;
for ( var i = 0; i < len; i++) {
document.write('<form:checkbox path="remoteSites" value="' + valueArray[i] + '" />');
}
});
};
</script>
and my Ajax request fails with 404 - no mapping found.
How can I make my REST url work. I know that I need a new view resolver, but I tried so many things and searching for right solution for past 2 days.
any help ?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能完全偏离这里 - 我看到您请求的网址 - $.getJSON('/aaa...) 没有添加上下文网址,这可能是您收到 404 的原因,除非您使用根上下文。无论哪种方式,我都会建议您使用 spring:url 或 c:url 来获取变量的 url 并使用它。
更新 1:再次查看您的 RequestMapping -
这是不正确的,您必须以这种方式指定它 - 没有 $ 符号:
另外,您可以在跟踪模式下运行它,并查看 Dispatcher servlet 在进行此更改后会输出什么内容。
May be totally off here - I see that the url that you are requesting to - $.getJSON('/aaa...), does not have the context url added to it, that could be the reason why you are getting a 404, unless you are using the root context. Either way I would recommend you to use spring:url or c:url to get the url to a variable and use that instead.
Update 1: Looking at your RequestMapping once more -
That is not correct, you have to specify it this way - without the $ sign:
Also, can you run it in trace mode and see what Dispatcher servlet spews out with this change in place.