Ajax 从REST Spring MVC Web应用发送HTML/JSON
我已经用SpringMVC开发了一个RESTWeb应用程序,我可以向客户机发送JSON对象 我想构建一个Javascript/AJAX客户端,连接到我的web应用程序,但我不知道如何发送第一个HTML页面(使用JSP)。 我知道我应该为JSP页面提供一些嵌入式AJAX。这个AJAX将向我的web服务发送请求 更新: 我无法实现的要求是编写默认URI(Ajax 从REST Spring MVC Web应用发送HTML/JSON,ajax,json,rest,spring-mvc,Ajax,Json,Rest,Spring Mvc,我已经用SpringMVC开发了一个RESTWeb应用程序,我可以向客户机发送JSON对象 我想构建一个Javascript/AJAX客户端,连接到我的web应用程序,但我不知道如何发送第一个HTML页面(使用JSP)。 我知道我应该为JSP页面提供一些嵌入式AJAX。这个AJAX将向我的web服务发送请求 更新: 我无法实现的要求是编写默认URI(http://localhost:8084)并查看我在JSP页面中编写的HTML页面(home.JSP) 我的做法如下: 我有一个控制器,它发送根J
http://localhost:8084
)并查看我在JSP页面中编写的HTML页面(home.JSP
)
我的做法如下:
我有一个控制器,它发送根JSP页面
@Controller
public class SessionController {
@RequestMapping(value="/", method=RequestMethod.GET)
public String homeScreen(){
return "home";
}
}
但是当我运行服务器时,我收到了这个警告
WARNING: No mapping found for HTTP request with URI [/home] in DispatcherServlet with name 'dispatcher'
浏览器中没有加载任何内容
这是我的应用程序上下文文件:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="com.powerelectronics.freesun.web" />
<mvc:annotation-driven />
</beans>
和web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
上下文配置位置
/WEB-INF/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
调度员
org.springframework.web.servlet.DispatcherServlet
2.
调度员
/*
30
我的方法正确吗?我在任何基本概念上都错了吗?
我可以修改代码中的某些内容使其运行吗?
我想在浏览器中看到加载的第一页,并继续朝那个方向前进
提前感谢。尝试在方法上添加
@ResponseBody
注释:
@Controller
public class SessionController {
@RequestMapping(value="/", method=RequestMethod.GET)
@ResponseBody
public String homeScreen(){
return "home";
}
}
这将在页面上输出home
如果您想使用视图技术,例如JSP,请查看官方Spring框架文档的以下章节:
更新
“就像与Spring集成的任何其他视图技术一样,对于JSP,您需要一个视图解析器来解析您的视图”。如果您想使用JSP,则应将以下内容添加到Web应用程序上下文中,然后返回要处理的文件名:
<!-- the ResourceBundleViewResolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basename" value="views"/>
</bean>
<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"/>
</bean>
上述内容是否存在于您的Web应用程序上下文中?您可以查看官方文档以了解更多信息:在web.xml文件中放置欢迎文件标记
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<beans:bean id="jacksonMessageChanger" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<beans:property name="supportedMediaTypes" value="application/json" />
</beans:bean>
<beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<beans:property name="messageConverters">
<util:list id="beanList">
<beans:ref bean="jacksonMessageChanger" />
</util:list>
</beans:property>
</beans:bean>
这将调用您的主页jsp
若要从spring控制器返回JSON,则需要在spring上下文xml文件中初始化jackson映射器bean
<beans:bean id="jacksonMessageChanger" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<beans:property name="supportedMediaTypes" value="application/json" />
</beans:bean>
<beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<beans:property name="messageConverters">
<util:list id="beanList">
<beans:ref bean="jacksonMessageChanger" />
</util:list>
</beans:property>
</beans:bean>
您需要添加jar或maven依赖项才能使用jackson映射器
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.8.5</version>
</dependency>
org.codehaus.jackson
杰克逊地图绘制者
1.8.5
要从控制器返回JSON,方法如下:
@RequestMapping(value="/getContacts", method=RequestMethod.GET)
public @ResponseBody List<Contacts> getContacts(){
List<Contacts> contactList = prepareContactList();
return contactList;
}
@RequestMapping(value=“/getContacts”,method=RequestMethod.GET)
public@ResponseBody List getContacts(){
List contactList=prepareContactList();
返回联系人列表;
}
通过这种方式,您将以对象的形式在ajax调用的成功函数中获得列表,通过迭代可以获得详细信息。最后,我仅通过以不同的方式在web.xml中配置dispatcher来解决此问题 首先,我按照David Riccitelli的建议将视图解析器添加到servlet配置文件中:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
然后我在web.xml中配置了servlet映射:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
调度员
/
这就是我想要的,不需要额外的配置。
这样做,我调用我的根URLhttp://localhost:8084
我可以看到我在home.jsp中编码的主屏幕
感谢您的支持和建议。您在浏览器中尝试了什么URL?看起来你是在请求什么?但是您还没有为“/home”设置请求映射,只有“/”[是我的URL。我想做的是将主屏幕作为JSP页面发送,但我不想看到这样的URL:[或类似的东西。我已经检查过,现在我可以像这样发送HTML,但在其他项目中,我会像Spring文档所说的那样自动发送JSP页面。问题是,我不知道为什么我不能使用此配置。请检查答案中的上述更新。请注意,Spring Web应用程序上下文必须配置为处理JSP的red:“就像您正在与Spring集成的任何其他视图技术一样,对于JSP,您需要一个视图解析器来解析您的视图”。有关更多信息,请参阅。我已检查了您建议的此配置,但对我不起作用。我知道我可以使用InternalResourceViewResolver将请求的html页面映射到JSP文件。我以前做过。但我想做的是打开我的根地址(我项目的默认URI)并查看我在一个JSP文件中编码的主屏幕。我仍然无法做到这一点。是的,我添加了Jackson依赖项,但Spring在path中自动识别。不需要在上下文文件中初始化它。我想要的是在根URL中看到home,而不是在浏览器中看到任何/home
URL。我将分享最后的做法我解决它。
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>