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/" ?
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.
– Alexandre
which URL displayed in the browser now?
– nullptr
now displays. Ex: "http://localhost:8080/project-example/hello/controller/" but I would like it to display http://localhost:8080/hello/controller/
– Alexandre
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?
– nullptr
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
– Alexandre
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.– nullptr
Also post page mapping to your folder
views
– nullptr
I edited the post and put the contents of the file "ctx-web-application-context.xml" and "web.xml"
– Alexandre