0
votes

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>
5
You can not access WEB-INF files directly .try to access localhost:8080/APK_downloader/hellorupesh_padhye
Please read this question, it'll help you a lot.Roger Dwan
@rupesh_padhye I don't want access with url, is any way to access jsp through controller ?Txuver Jhi Wei

5 Answers

2
votes

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

1
votes

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

@RequestMapping(value = "/hello")
1
votes

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.

0
votes

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)
0
votes

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.