0

I have developed a REST web application with Spring MVC and I can send JSON objects to a client.

I would like to construct a Javascript/AJAX client that connects to my web application but I don't know how to send the first HTML page (using JSP). I understand I should serve JSP pages with some embedded AJAX. This AJAX will send requests to my web services.

Update: The requirement I am not able to achieve is to write the default URI (http://localhost:8084) in browser and see the HTML page I have written in JSP page (home.jsp).

My approach is following:

I have a Controller that sends the root JSP page

@Controller
public class SessionController {

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

But when I run the server I receive this warning

WARNING: No mapping found for HTTP request with URI [/home] in DispatcherServlet with name 'dispatcher'

and nothing is loaded in browser.

Here is my application-context file:

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

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

Is my approach correct? Am I wrong at any basic concept? Can I modify something in my code to make it run? I would like to see the first page loaded in browser and keep going in that direction.

Thanks in advance.

Spacemonkey
  • 1,725
  • 3
  • 20
  • 44
  • 1
    What URL were you trying in the browser? It looks like you were requesting http://localhost:8080/your_optional_context/home? But you haven't set-up a request mapping for "/home", only "/". – nickdos Nov 01 '12 at 00:48
  • [http://localhost:8084/] is my URL. What I want to do is to send the home screen as JSP page but I don't want to see an URL like this: [http://localhost:8084/home.htm] or something similar. – Spacemonkey Nov 01 '12 at 18:05

3 Answers3

0

Try adding a @ResponseBody annotation on the method:

@Controller
public class SessionController {

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

This should output home on the page.

If you'd like to use View technologies, e.g. JSP, review the following chapter on the official Spring Framework documentation: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#view

Update

"Just as with any other view technology you're integrating with Spring, for JSPs you'll need a view resolver that will resolve your views ". If you'd like to use JSP you should then add the following to your Web application context, then return the name of the file that shall be processed:

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

Is the above present in your Web application context? You can review the official documentation for further information: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#view-jsp-resolver

David Riccitelli
  • 7,491
  • 5
  • 42
  • 56
  • I have checked and I now I can send HTML like this but in other projects I have send JSP pages automatically like Spring documentation says. The question is that I don't know why I can't do it with this configuration. – Spacemonkey Nov 01 '12 at 18:09
  • Please check the above update in the answer. Be aware that the Spring Web application context **must be configured to process JSPs**: "Just as with any other view technology you're integrating with Spring, for JSPs you'll need a view resolver that will resolve your views". For further information refer to http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#view-jsp-resolver – David Riccitelli Nov 01 '12 at 22:13
  • I have checked this configuration you suggests but is not working to me. I know I can map requested html pages to JSP files using InternalResourceViewResolver. I did it in the past. But what I would like to do is to open my root address (default URI of my project) and see the home screen I have coded in one JSP file. I am still not able to do that. – Spacemonkey Nov 02 '12 at 10:23
0

Place a welcome file tag in web.xml file.

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

You must be having this index.jsp outside of WEB-INF. Put following code in it.

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

When application is loaded, it will call index.jsp and jsp will redirect it to /home action.

Then your controller will get called.

@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";
    }
}

This will call your home jsp.

If you want to to return JSON from your spring controller, then you need jackson mapper bean initialized in spring context xml file.

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

You need to add jar or maven dependency to use jackson mapper.

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

And to return JSON from controller, method would be like this :

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

This way you will get List in the success function of ajax call in the form of object and by iterating it you can get the details.

Jeevan Patil
  • 6,029
  • 3
  • 33
  • 50
  • Yes, I have added Jackson dependency but Spring does recognize automatically in path [link](http://stackoverflow.com/a/10324550/1242531). It was not necessary to initialize it in context file. What I wanted is to see home in root URL, not to see any `/home` URL in browser. I will share how finally did I solve it. – Spacemonkey Nov 05 '12 at 14:44
0

Finally I solve this only with configuring the dispatcher in web.xml on a different way.

First I added the view resolver to the servlet configuration file as David Riccitelli suggested me:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>

And then I configured the servlet mapping in web.xml:

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

That's what I was looking for, and no extra configuration is needed. Doing this I call my root URL http://localhost:8084 and I can see the home screen I have coded in home.jsp.

Thanks for your support and suggestions.

Spacemonkey
  • 1,725
  • 3
  • 20
  • 44