Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 4 can';t呈现jsp_Spring_Jsp_Spring Mvc - Fatal编程技术网

Spring mvc 4 can';t呈现jsp

Spring mvc 4 can';t呈现jsp,spring,jsp,spring-mvc,Spring,Jsp,Spring Mvc,我试图学习Spring MVC 4,但不使用web.xml,而是使用以下内容: public class WebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { WebApplicationContext context = getContext();

我试图学习Spring MVC 4,但不使用web.xml,而是使用以下内容:

public class WebAppInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    WebApplicationContext context = getContext();
    servletContext.addListener(new ContextLoaderListener(context));
    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("*.html");
}

private AnnotationConfigWebApplicationContext getContext() {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.setConfigLocation("com.bookstr.WebConfig");
    return context;
}

}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home</title>
</head>
<body>
${home}
</body>
</html>

由于某些原因,当我使用以下控制器时,我创建并放置在src->main->webapp文件夹中的home.jsp文件将不会呈现:

@Controller
public class HomeController {

    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public String getHome(Model model){
        model.addAttribute("home", "Hello world");

        return "home";
    }
}
JSP文件如下所示:

public class WebAppInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    WebApplicationContext context = getContext();
    servletContext.addListener(new ContextLoaderListener(context));
    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("*.html");
}

private AnnotationConfigWebApplicationContext getContext() {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.setConfigLocation("com.bookstr.WebConfig");
    return context;
}

}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home</title>
</head>
<body>
${home}
</body>
</html>

家
${home}
更新

文件夹结构符合要求


我可以看到HomeController没有被调用。

在您的
网络配置中注册
viewsolver

@Configuration
@EnableWebMvc
@ComponentScan("com.bookstr")
public class WebConfig {
    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResover = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");

        return viewResolver;
    }
}
这样,当您从控制器返回
主页
时,它将尝试呈现
/WEB-INF/views/home.jsp
视图。要以编程方式配置DispatcherServlet
,使用
AbstractAnnotationConfigDispatcherServletInitializer
更容易。例如,您可以:

public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { WebConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/*.html" };
    }
}
公共类WebInitializer扩展AbstractAnnotationConfigDispatcherServletInitializer{
@凌驾
受保护类[]getRootConfigClasses(){
返回null;
}
@凌驾
受保护类[]getServletConfigClasses(){
返回新类[]{WebConfig.Class};
}
@凌驾
受保护的字符串[]getServletMappings(){
返回新字符串[]{”/*.html“};
}
}

在您的
网络配置中注册
viewsolver

@Configuration
@EnableWebMvc
@ComponentScan("com.bookstr")
public class WebConfig {
    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResover = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");

        return viewResolver;
    }
}
这样,当您从控制器返回
主页
时,它将尝试呈现
/WEB-INF/views/home.jsp
视图。要以编程方式配置DispatcherServlet
,使用
AbstractAnnotationConfigDispatcherServletInitializer
更容易。例如,您可以:

public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { WebConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/*.html" };
    }
}
公共类WebInitializer扩展AbstractAnnotationConfigDispatcherServletInitializer{
@凌驾
受保护类[]getRootConfigClasses(){
返回null;
}
@凌驾
受保护类[]getServletConfigClasses(){
返回新类[]{WebConfig.Class};
}
@凌驾
受保护的字符串[]getServletMappings(){
返回新字符串[]{”/*.html“};
}
}

您是否注册了
viewsolver
来解析您的jsp视图?不,我没有@alidehghanid您注册了
viewsolver
来解析您的jsp视图吗?不,我没有@alidehghania您确定您的控制器被调用了吗?在控制器中设置断点并确保这一点。另外,您把视图放在哪里了?注册解析器并在
src/main/webapp/WEB-INF/views/home.jsp
中创建
home.jsp
。另外,在标记的
src/main
中添加
webapp
文件夹。您确定您的控制器被呼叫了吗?在控制器中设置断点并确保这一点。另外,您把视图放在哪里了?注册解析器并在
src/main/webapp/WEB-INF/views/home.jsp
中创建
home.jsp
。另外,在标记的
src/main
中添加
webapp
文件夹。