我既不使用ResponseBody
也不使用RestController
批注我的Spring应用程序仍在返回String而不是jsp / html页面。
这是我的应用程序配置和控制器文件。
我要去哪里错了?
我的代码的GIT link
Web配置文件:
package com.springimplant.mvc.config;
import java.io.IOException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
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.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.ResourceBundleViewResolver;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.XmlViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.springimplant.mvc.controllers")
public class SimpleWebConfiguration implements WebMvcConfigurer {
@Bean
public ViewResolver internalResourceViewResolver() {
// UrlBasedViewResolver bean = new UrlBasedViewResolver();
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/views/");
bean.setSuffix(".jsp");
bean.setOrder(0);
return bean;
}
@Bean
public ViewResolver resourceBundleViewResolver() {
ResourceBundleViewResolver bean = new ResourceBundleViewResolver();
bean.setBasename("views");
bean.setOrder(1);
return bean;
}
@Bean
public ViewResolver xmlViewResolver(){
XmlViewResolver bean = new XmlViewResolver();
bean.setLocation(new ClassPathResource("views.xml"));
bean.setOrder(2);
return bean;
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/views/", ".jsp");
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// registry.addViewController("/").setViewName("forward:/welcome");
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
家用控制器:
package com.springimplant.mvc.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/")
public class HomeController {
@RequestMapping(value="welcome",method=RequestMethod.GET)
public ModelAndView welcome()
{
return new ModelAndView("welcome");
}
}
最佳答案
<%@ 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>Welcome Home</title>
</head>
<body>
Welcome Home
</body>
</html>
所以我将这些页面标签放在我删除的jsp文件上,现在可以正常运行了,但是为什么呢?
在我以前的项目中,我不必这样做。