Page displaying error

Asked

Viewed 57 times

3

I am beginner in spring with java and have the following controller and jsp

Controller:

@Controller
//mapeamento do nome
@RequestMapping("/hello")
public class HelloController {

    //mapeamento do nome
    @RequestMapping("/controller")
    public ModelAndView hello() {

        //caminho da pagina .jsp
        return new ModelAndView("/hello/view", "message", "Bem-vindo ao spring");
    }
}

Pagina index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!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=UTF-8">
        <title>Insert title here</title>
    </head>
    <body>
        <a href="/hello/controller/"> Hello </a>
        <br />
        <a href="index.jsp"> Teste 1</a>
        <br />
    </body>
</html>

Page view.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!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=UTF-8">
        <title>Spring MVC</title>
    </head>
    <body>

        <h2>${message }</h2>
    </body>
</html>

The problem is. I normally start the application on Tomcat. Access, "http://localhost:8080/project-example/" and the page displays correctly. However, when I click on the link 'Hello', it redirects to the link "http://localhost:8080/hello/controller/" ... correct, HOWEVER, displays the error message:

"HTTP Status 404 - Not Found Type Status Report

Message /hello/controller/

Description The origin server Did not find a Current representation for the target Resource or is not willing to disclose that one exists."

But if I put the direct link as "http://localhost:8080/project-example/hello/controller/" The page is displayed correctly.

In case, how do I fix the issue and display the page correctly when the link is redirected to "http://localhost:8080/hello/controller/" ?

2 answers

2

In the link of your JSP, you should indicate the context of your application, as the example below:

<a href="${pageContext.request.contextPath}/hello/controller/"> Hello </a>

The expression will concatenate the context of your application to the link, directing to the correct address.


UPDATE: For your application configuration to go directly to the localhost:8080 URL, you should deploy directly to the server’s ROOT context. For this you can change the context of your application in the file server.xml, on the tag context you will see the context of your application, this way just change to /.

In the Eclipse: Server Tab > Modules > Change the application context to /

Important: If you change the context of your application to directly use the root, the ${pageContext.request.contextPath} will no longer be needed as stated above.


UPDATE: As after including the ${pageContext.request.contextPath} you managed to get to your @Controller, if you are still displaying 404 is due to incorrect targeting to your page view.jsp

Try it the way below:

return new ModelAndView("hello/view", // Trocando de /hello/view para hello/view ...

  • I put the specified command, but displayed the error message in jsp In this case, I changed my path by placing a dot. Ex: <a href=". /hello/controller/"> Hello </a> However, I don’t know if what I did is feasible.

  • which URL displayed in the browser now?

  • now displays. Ex: "http://localhost:8080/project-example/hello/controller/" but I would like it to display http://localhost:8080/hello/controller/

  • The only way to present directly to localhost:8080 is to deploy directly to the server’s ROOT context. Another issue, you checked if debugging the request is reaching the controller?

  • Got it. Yes, I debugged it and I accessed the controller when I put "." in the path. Without "." it can no longer access the controller

  • Right then, accessing the controller, if still continue the 404 the problem is in redirecting to the view.jsp, try to follow the instructions I updated in the reply, and also post the folder structure of your project and pages.

  • Also post page mapping to your folder views

  • I edited the post and put the contents of the file "ctx-web-application-context.xml" and "web.xml"

Show 3 more comments

0

I am sending the image with the project folder structure

inserir a descrição da imagem aqui

In the Servers tab, I ended up not finding the options you mentioned

inserir a descrição da imagem aqui

ctx-web-application-context.xml 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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc 
    	http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	<context:component-scan base-package="br.com.projetoexemplo.controller" />
	
	<mvc:annotation-driven />

	<bean id="viewResolver"	class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		
		<property name="prefix" value="/views/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

Web file.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">

	<!-- DEFAULT PAGE -->
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<!-- SPRING MVC -->
	<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/ctx-web-application-context.xml</param-value>
        </init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

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

</web-app>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.