Java 使用Spring3和Tiles 2对web应用程序的Ajax REST调用
我有一个使用Spring3和tiles 2(不是RESTful)构建的webapp。我正在使用UrlBasedViewResolver显示视图 我的应用程序-servlet.xmlJava 使用Spring3和Tiles 2对web应用程序的Ajax REST调用,java,spring,tiles,spring-3,apache-tiles,Java,Spring,Tiles,Spring 3,Apache Tiles,我有一个使用Spring3和tiles 2(不是RESTful)构建的webapp。我正在使用UrlBasedViewResolver显示视图 我的应用程序-servlet.xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframewo
<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>
/WEB-INF/tiles.xml
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>
现在我的问题是——我在客户端有一个动态表单,它是根据用户选项动态生成的(使用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;
}
}
@控制器
公共类UtilsController{
私人网站管理;
public UtilsController()引发WebAdministrationException{
this.admin=WebAdministration.getInstance();
}
@RequestMapping(value=“/aaaa/${site}/${type}”,method=RequestMethod.GET,headers=“Accept=text/xml,application/json”)
public@ResponseBody List getRemoteSites(@PathVariable(“site”)字符串site,@PathVariable(“type”)字符串类型){
List remoteSites=null;
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();
}
返回远程站点;
}
}
我的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>
函数getRemoteSites(站点,类型){
$.getJSON('/aaaa/'+site+'/'+type{
阿贾克斯:“真的”
},函数(数据){
var len=data.length;
对于(变量i=0;i
我的Ajax请求因404而失败-未找到映射
我如何使我的REST url工作。我知道我需要一个新的视图解析器,但在过去的两天里,我尝试了很多方法并寻找正确的解决方案
有什么帮助吗
谢谢这里可能完全不存在-我看到您请求的url-$.getJSON('/aaa…)没有添加上下文url,这可能是您获得404的原因,除非您使用的是根上下文。无论采用哪种方式,我都建议您使用spring:url或c:url来获取变量的url,并改用它
<spring:url value="/aaaa/" var="myurl" />
这是不正确的,您必须以这种方式指定它-不带$符号:
@RequestMapping(value="/aaaa/{site}/{type}"
另外,您可以在跟踪模式下运行它,并查看DispatcherServlet在进行此更改时输出了什么 这里可能完全不正确-我看到您请求的url-$.getJSON('/aaa…)没有添加上下文url,这可能是您获得404的原因,除非您使用的是根上下文。无论采用哪种方式,我都建议您使用spring:url或c:url来获取变量的url,并改用它
<spring:url value="/aaaa/" var="myurl" />
这是不正确的,您必须以这种方式指定它-不带$符号:
@RequestMapping(value="/aaaa/{site}/{type}"
另外,您可以在跟踪模式下运行它,并查看DispatcherServlet在进行此更改时输出了什么 我没有在这里提到上下文url,但我有它。我试着把整个url放到浏览器中,它应该会返回一些东西,它说404,我的spring记录错误[19.01.2012 20:16:14]WARN-(http-bio-8080-exec-3)org.springframework.web.servlet.DispatcherServlet.noHandlerFound(DispatcherServlet.java:947)-找不到URI为http请求的映射[/web-spring/aaaaaaaa/mySite/siteType]在名为“web管理员”的DispatcherServlet中。所以,我确信url不起作用了,谢谢。我在我的帖子中添加了一个更新-看起来你的路径变量有一个额外的$符号。只是修复了它。我很惊讶我从未注意到这一点。谢谢,我没有在这里提到上下文url,但我有它。我试着把整个url放到浏览器中,它应该会返回一些东西,它说404,我的spring记录错误[19.01.2012 20:16:14]WARN-(http-bio-8080-exec-3)org.springframework.web.servlet.DispatcherServlet.noHandlerFound(DispatcherServlet.java:947)-找不到URI为http请求的映射[/web-spring/aaaaaaaa/mySite/siteType]在名为“web管理员”的DispatcherServlet中。所以,我确信url不起作用了,谢谢。我在我的帖子中添加了一个更新-看起来你的路径变量有一个额外的$符号。只是修复了它。我很惊讶我从未注意到这一点。谢谢