【问题标题】:JSP output plain text in Web BrowserJSP在Web浏览器中输出纯文本
【发布时间】:2015-03-16 22:57:59
【问题描述】:

我正在使用 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";
    }
}

【问题讨论】:

  • 它正在输出&lt;%@page...等?
  • 是的,它正在输出
  • 您是如何创建 JSP 文件的?它在您的项目中的什么位置?您如何访问它(您使用的是什么 URL)?
  • 我使用 Eclipse 菜单创建它,并将其放置在 WebContent/WEB-INF/jsp/ 中,我在运行时使用 / 访问它
  • 你是如何访问它的?

标签: java spring jsp


【解决方案1】:

控制器有一个 GET 和一个 POST 请求方法。但是快速浏览一下,您需要将 @RequestMapping("/greeting") 更改为 @RequestMapping(value = "/greeting") 以供初学者使用。默认情况下,您的 jsp 文件应位于 /src/main/webapp/WEB-INF/views (Spring MVC Starter Project)

当您返回一个字符串时 - 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 是最常用且最容易理解的。

【讨论】:

  • @RequestMapping("/path") === @RequetMapping(value="/path")
【解决方案2】:

可能你唯一缺少的就是

在jsp页面右击,点击RUN AS,然后RUN ON SERVER。

【讨论】:

    猜你喜欢
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-13
    • 2022-01-09
    相关资源
    最近更新 更多