Java Spring:JSP没有读取传递的ModelMap?
我试图让我的JSP文件使用servlet传递的ModelMap,但得到的是一个空字符串。已经试着调试了一段时间,但是没有任何进展。有人知道这个问题吗?我正在使用最新的SpringJava Spring:JSP没有读取传递的ModelMap?,java,xml,spring,servlets,spring-mvc,Java,Xml,Spring,Servlets,Spring Mvc,我试图让我的JSP文件使用servlet传递的ModelMap,但得到的是一个空字符串。已经试着调试了一段时间,但是没有任何进展。有人知道这个问题吗?我正在使用最新的SpringWebInitializer类index.jsp为我打印Hello而不是Hello Bob index.jsp <!DOCTYPE html> <html> <body> Hello ${test} </body> </html> WebInitializer
WebInitializer
类index.jsp为我打印Hello
而不是Hello Bob
index.jsp
<!DOCTYPE html>
<html>
<body>
Hello ${test}
</body>
</html>
WebInitializer.java
@Controller
@RequestMapping("/")
public class HomeController {
@RequestMapping(method = RequestMethod.GET)
public String foo(ModelMap modelMap) throws Exception {
modelMap.addAttribute("test", "Bob");
return "index";
}
}
public class WebInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.scan("com.myProject.bootstrap");
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(dispatcherContext));
//Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
Set<String> mappingConflicts = dispatcher.addMapping("/views/*");
}
}
@Configuration
@ComponentScan(basePackages = {"com.myProject"})
@EnableWebMvc
public class AppConfiguration {
@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
internalResourceViewResolver.setPrefix("/WEB-INF/views/");
internalResourceViewResolver.setSuffix(".jsp");
return internalResourceViewResolver;
}
}
web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
</web-app>
我的webapp文件夹树
尝试返回“转发:索引”
您还可以将处理程序的返回类型更改为“ModelAndView”,并在返回ModelAndView对象时设置视图名称和模型。尝试返回“forward:index”
您还可以将处理程序的返回类型更改为“ModelAndView”,并在返回ModelAndView对象时设置视图名称和模型。据我所知,您正在尝试创建一个非常基本的spring应用程序。请尝试此网站:。您无需为此编写大量代码。Spring非常强大。据我所知,您正在尝试创建一个非常基本的Spring应用程序。请尝试此网站:。您无需为此编写大量代码。Spring非常强大。感谢您的帮助,实际上我在JSP页面中使用了错误的属性名称。。。所以我现在可以用ModelMap了。感谢您的帮助,实际上我在JSP页面中使用了错误的属性名称。。。所以我现在可以用ModelMap了。它也适用于ModelAndView。