我目前正在尝试从控制器向视图中显示变量,视图没有显示它们。
感谢您的帮助!
UserController.java
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value="/user")
public class UserController {
@RequestMapping(method = RequestMethod.GET)
public String login(ModelMap model) {
model.put("message", "added Var from controller");
return "user/login";
}
}
也试过
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("user/login");
modelAndView.addObject("message", "Hello World");
return modelAndView;
JSP
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<h2>Login</h2>
<p>This is my message: ${message}</p>
<c:out value="${message}" />
</body>
</html>
输出
<html>
<body>
<h2>Login</h2>
<p>This is my message: ${message}</p>
${message}
</body>
</html>
My Spring servlet-context.xml
<context:component-scan base-package="com.web.controllers" />
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/view/" />
<property name="suffix" value=".jsp" />
</bean>
我有什么问题吗?