Page displaying an error

3

I'm new to spring with java and I 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");
    }
}

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

View.jsp page

<%@ 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, " link " and the page displays correctly. But when I click on the 'Hello' link, it redirects to the link " link " ... correct, POREM, 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 " link " The page is displayed correctly.

In this case, how do I correct the problem and correctly display the page when the link is redirected to " link ?

    
asked by anonymous 01.01.2019 / 15:29

2 answers

2

In the JSP link, you should indicate the context of your application, as shown 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: Regarding your application's configuration go directly to the localhost URL: 8080, you should deploy directly to the server's ROOT context. To do this you can change the context of your application in the server.xml file, in the context tag you will see the context of your application, so just change to / .

In Eclipse: Aba Server > Modules > Change application context to /

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

UPDATE : Since after including ${pageContext.request.contextPath} you were able to get to your @Controller , if you are still showing 404 is due to incorrect targeting to your page view.jsp

Try the form below:

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

    
01.01.2019 / 17:13
0

I am sending the image with the project folder structure

OntheServerstab,Ididnotfindtheoptionsyoumentioned

ctx-web-application-context.xmlfile

<?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.xml file

<?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>
    
01.01.2019 / 23:15