从REST Spring MVC Web应用程序发送HTML / JSON

时间:2012-10-31 17:04:05

标签: ajax json rest spring-mvc

我使用Spring MVC开发了一个REST Web应用程序,我可以将JSON对象发送给客户端。

我想构建一个连接到我的Web应用程序的Javascript / AJAX客户端,但我不知道如何发送第一个HTML页面(使用JSP)。 我知道我应该使用一些嵌入式AJAX来提供JSP页面。这个AJAX会向我的网络服务发送请求。

更新 我无法实现的要求是在浏览器中编写默认URI(http://localhost:8084)并查看我在JSP页面(home.jsp)中编写的HTML页面。

我的方法如下:

我有一个发送根JSP页面的 Controller

@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>

我的方法是否正确?我对任何基本概念都错了吗? 我可以修改代码中的某些内容以使其运行吗? 我希望看到第一页在浏览器中加载并继续向这个方向发展。

提前致谢。

3 个答案:

答案 0 :(得分:0)

尝试在方法上添加@ResponseBody注释:

@Controller
public class SessionController {

    @RequestMapping(value="/", method=RequestMethod.GET)
    @ResponseBody
    public String homeScreen(){
        return "home";
    }
}

这应该在页面上输出home

如果您想使用查看技术,例如JSP,回顾以下关于官方 Spring Framework文档的章节:http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#view

<强>更新

“正如您正在与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应用程序上下文中是否存在上述内容?您可以查看官方文档以获取更多信息:http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#view-jsp-resolver

答案 1 :(得分:0)

在web.xml文件中放置一个欢迎文件标记。

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

您必须在WEB-INF之外拥有此index.jsp。将以下代码放入其中。

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Insert title here</title>
    </head>
    <body>
        <%
            response.sendRedirect("home");
        %>
    </body>
</html>

加载应用程序时,它将调用index.jsp,jsp会将其重定向到/ home action。

然后你的控制器就会被召唤。

@Controller
public class SessionController {

    // see the request mapping value attribute here, it is /home

    @RequestMapping(value="/home", method=RequestMethod.GET)
    public String homeScreen(){
        return "home";
    }
}

这会打电话给你的家庭jsp。

如果你想从spring控制器返回JSON,那么你需要在spring context xml文件中初始化jackson mapper 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 mapper。

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.8.5</version>
</dependency>

要从控制器返回JSON,方法将是这样的:

@RequestMapping(value="/getContacts", method=RequestMethod.GET)
public @ResponseBody List<Contacts> getContacts(){
        List<Contacts> contactList = prepareContactList();
        return contactList;
}

通过这种方式,您将以对象的形式获得ajax调用的成功函数中的List,并通过迭代它可以获得详细信息。

答案 2 :(得分:0)

最后,我只是通过以不同的方式在web.xml中配置调度程序来解决这个问题。

首先我将视图解析器添加到servlet配置文件中,David Riccitelli建议我:

<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>

这就是我要找的东西,不需要额外的配置。 这样做我调用我的根URL http://localhost:8084,我可以看到我在home.jsp编码的主屏幕。

感谢您的支持和建议。