2

I am using Eclipse, Spring MVC, Maven and Tomcat. This index.jsp displays exactly as show below in the web browser. It is not rendering properly.

Any idea what is wrong?

index.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>Index</h1>
</body>
</html>

@Controller
public class HelloController {

    @RequestMapping("/greeting")
    public String sayHello() {
        System.out.println("Greeting");
        return "hello";
    }

    @RequestMapping("/")
    public String index() {
        System.out.println("Index page");
        return "index";
    }
}
AstroCB
  • 12,337
  • 20
  • 57
  • 73

2 Answers2

0

A controller has a GET and a POST RequestMethod.However at a quick glance you need to change @RequestMapping("/greeting") to @RequestMapping(value = "/greeting") just for starters. By default your jsp file should be in /src/main/webapp/WEB-INF/views (Spring MVC Starter Project)

When you return a String - Spring MVC will look for a jsp with that .jsp. So in this example you just want to have greeting.jsp

@Controller
public class GreetingController {

 /**
  * GET
  */

@RequestMapping(value = "/greeting", method = RequestMethod.GET)
public String handleRequest() {
// This will be used when you GET the URL
  return "greeting";
 }

 /**
  * POST
  */
 @RequestMapping(value = "/greeting", method = RequestMethod.POST)
 public String processSubmit(){

  // This will be used when you POST to the URL
  //TODO Do something here and it will put you right back in your page

  return "greeting";
 }
}

Go ahead and comment if you have any other questions. Also check my account for my other example Neither BindingResult nor plain target object for bean name available as request attr

Hope this helps. Good Luck!

Just a note Spring has more RequestMethod's but GET and POST are the most used and easiest to understand.

Community
  • 1
  • 1
Dan Hargis
  • 457
  • 5
  • 13
0

It could be that the only thing you're missing is to

Right click on the jsp page and click RUN AS, then RUN ON SERVER.

Kennedy
  • 27
  • 8