0
遵循幾個示例,仍然無法使其工作。無法生成JSP顯示模型值。簡單的Spring MVC示例
使用基本的Spring MVC,嘗試顯示控制器返回的一些數據。 (使用Spring 4.3.9,Tomcat的20年5月8日和鉻)
這裏是項目樹:
.
├── pom.xml
└── src
└── main
├── java
│ └── mywebapp
│ ├── config
│ │ ├── MyWebAppInitializer.java
│ │ ├── RootConfig.java
│ │ └── WebConfig.java
│ └── web
│ └── HomeController.java
├── resources
└── webapp
└── WEB-INF
├── views
│ └── home.jsp
└── web.xml
1)MyWebAppInitializer.java
package mywebapp.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { RootConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebConfig.class };
}
}
2)RootConfig.java是空班,沒有豆子。
3)WebConfig.java - I使用兩個版本這個配置文件的: 版本答:
package mywebapp.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan("mywebapp.config")
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
版本B: - 所不同的是隻在視圖解析器()
package mywebapp.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.JstlView;
@Configuration
@EnableWebMvc
@ComponentScan("mywebapp.config")
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
4)HomeController.java
package mywebapp.web;
import static org.springframework.web.bind.annotation.RequestMethod.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;
@Controller
public class HomeController {
@RequestMapping(value="/", method=GET)
public String home(ModelMap model) {
model.addAttribute("xyz","this is my message");
return "home";
}
}
5)針對home.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
<h2>Hello World!</h2>
<c:out value="${xyz}" />
<br>${xyz}
</body>
</html>
6)的web.xml - 是 「空」 的,由行家原型被創建後沒有被修改。
對於WebConfig.java
輸出兩個版本是相同的:
Hello World!
${xyz}
${xyz}
請幫幫忙!
你試圖返回的ModelAndView和使用 「前進:指數」? –