0

I'm new to Spring MVC java.

Configuration ( what I've done )

  • import spring library
  • import common-logging
  • Tomcat server ( can access localhost:8080 )

Problem encounter

I can access index.jsp under web-content without problem, but when access hello.jsp under WEB-INF, server show HTTP STATUS 404, url was stopped at http://localhost:8080/APK_downloader/WEB-INF/jsp/hello.jspproblem image

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">

  <display-name>APK downloader</display-name>

   <servlet>
      <servlet-name>spring-dispatcher</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
   </servlet>

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

</web-app>

spring-dispatcher-servlet.xml (servlet configuration xml)

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-4.2.3.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-4.2.3.xsd">

    <context:component-scan base-package="solutionView"/>

    <bean id="HanlderMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

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

</beans>

DBController.java ( java resources class )

package solutionView;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "/hello")
public class DBController {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public ModelAndView getModelView(){
        System.out.println("helllo");    // check point
        return new ModelAndView("hello");
    }

}

hello.jsp ( view jsp )

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hello</h1>
<h2>${message}</h2>
</body>
</html>
3
  • 4
    You can not access WEB-INF files directly .try to access localhost:8080/APK_downloader/hello Commented Dec 2, 2015 at 7:09
  • 1
    Please read this question, it'll help you a lot. Commented Dec 2, 2015 at 7:17
  • @rupesh_padhye I don't want access with url, is any way to access jsp through controller ? Commented Dec 2, 2015 at 10:40

5 Answers 5

2

First thing is that you can't access WEB-INF folder directly . So http://localhost:8080/APK_Downloader/WEB-INF/jsp/hello.jsp will not work

Second you annotated your handler with @RequestMapping(value = "/hello") as well as your controller class. So the complete url for accessing the handler is

http://localhost:8080/APK_Downloader/hello/hello

if you directly want to access the method relative to your context just remove @RequestMapping(value = "/hello") from controller

and also change your view resolver defination

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

to

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

you missed the jsp part in prefix

Sign up to request clarification or add additional context in comments.

Comments

2

Change your spring-dispatcher-servlet.xml

From

<bean id="HanlderMapping" 
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

To

  <mvc:annotation-driven />

Because you are using annotation-driven MVC controllers (i.e. @RequestMapping, @Controller). That's why you need to add that above statement in spring-dispatcher-servlet.xml.

Comments

1

Please remove this line and direct write /hello in url string it is redirect to hello.jsp page

@RequestMapping(value = "/hello")

1 Comment

Thanks for correction, but it can't open as default hello.jsp But it help in future to redirect url page. @Controller @RequestMapping("/hello") Publicpublic class DBController { }
0

You can't access files under WEB-INF directly you have to let the servlet to manage that.

As per the URL mapping, you need to know that the mapping of a method comes after the mapping of its owning Controller

For Example :

@Controller
@RequestMapping(value = "/hello")
public class DBController {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public ModelAndView getModelView(){
        System.out.println("helllo");    // check point
        return new ModelAndView("hello");
    }

}

Means that the getModelView() will respond the the following URL :

http://localhost:8080/APK_downloader/hello/hello

to remove the duplicated hello you have two choices, either remove the

 @RequestMapping(value = "/hello") 

from the controller , or keep it and change your method to the following:

 @RequestMapping(value = "", method = RequestMethod.GET)

Comments

0

Change your controller class this way :

@Controller
public class DBController {

    @RequestMapping(value = "/hello")
    public String displayHelloPage(){
       return "hello";

       }

}

Now, how to call this URL. localhost:8080/context_path/hello. You must check if you have context path in eclipse set, in Servers. If Context-path is /, then just localhost:8080/hello . I hope this helps, if not, lemme know, I will remove my answer.

3 Comments

You let me know context path could change from project => properties => web project settings => Context root Thanks.
But I told you it's in Servers, as I use Intellij Idea, I couldn't remember where exactly it was/is.
Then your config must be wrong, edit your main post and paste spring-dispatcher-servlet.xml and web.xml, complete.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.