我正在尝试使用 Spring MVC
编写 Hello 页面。
Hello.jsp
<%@page isELIgnored="false"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>${hello.test()}</h1>
</body>
</html>
Controller 类:
package Controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class Hello {
@RequestMapping(value="/test.htm")
public String test()
{
return "Hello";
}
}
但是当我请求 http://localhost:8080/WebApplication1/test.htm
方法 test()
时,不会调用。有什么问题吗?
最佳答案
您不应从 View 中调用 Controller 方法。您可以使用 JSF 来完成此操作,但不能使用 JSP 来完成。看这个例子:
@Controller
public class Hello {
@RequestMapping(value="/test.htm")
public String test(Model uiModel){
uiModel.addAttribute("yourkey", "Hello World"); // put some data
return "Hello"; // means "open Hello.jsp"
}
}
现在您可以读取数据了:
<%@page isELIgnored="false"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>${yourkey}</h1>
</body>
</html>
关于java - Spring EL 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24740646/