JSP在Web浏览器中输出纯文本

时间:2015-03-16 22:57:59

标签: java spring jsp

我正在使用Eclipse,Spring MVC,Maven和Tomcat。此index.jsp与Web浏览器中的显示完全相同。它没有正确呈现。

知道出了什么问题吗?

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";
    }
}

2 个答案:

答案 0 :(得分:0)

控制器有一个GET和一个POST RequestMethod。但是,您需要快速将@RequestMapping(&#34; / greeting&#34;)更改为@RequestMapping(value =&#34; / greeting&#34; )仅限初学者。默认情况下你的jsp文件应该在/ src / main / webapp / WEB-INF / views(Spring MVC Starter Project)中

当你返回一个String时 - Spring MVC会查找带有.jsp的jsp。所以在这个例子中你只想拥有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";
 }
}

如果您有任何其他问题,请继续评论。另请查看我的帐户,查看我的其他示例Neither BindingResult nor plain target object for bean name available as request attr

希望这会有所帮助。祝你好运!

请注意Spring有更多的RequestMethod,但GET和POST是最常用和最容易理解的。

答案 1 :(得分:0)

可能您唯一想念的就是

右键单击jsp页面,然后单击“运行方式”,然后单击“在服务器上运行”。