【问题标题】:Spring mvc jsp not renderingSpring mvc jsp不渲染
【发布时间】:2016-08-29 21:09:29
【问题描述】:

开始使用 Spring MVC,当我进入我的页面时,它会将welcome.jsp 加载为纯文本(仅显示源代码),我知道人们问了这 100 万次,但我查看了很多问题并没有找到我的解决方案,因为他们中的大多数都使用 XML,而我使用 java.. 我仍然没有足够的经验在两者之间切换。

conf.class:

public class conf extends WebMvcConfigurerAdapter {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public InternalResourceViewResolver jspViewResolver() {
        InternalResourceViewResolver bean = new InternalResourceViewResolver();
        bean.setViewClass(JstlView.class);
        bean.setPrefix("/views/");
        bean.setSuffix(".jsp");
        return bean;
    }
}

ServletInitializer.class:

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

        return application.sources(Demo4Application.class,conf.class);
    }
}

welCont.class(控制器):

@Controller
@RequestMapping("/spring/")
public class welCont {

    @RequestMapping(method = RequestMethod.GET)
    public String wel(ModelMap model)
    {
        model.addAttribute("test","testme");
        return "welcome";
    }
}

欢迎.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>${test}</h2>
</body>
</html>

【问题讨论】:

    标签: spring jsp spring-mvc


    【解决方案1】:

    我认为错误的是

    @RequestMapping("/spring/")
    

    通常“/spring/”请求应该发送到 DispatcherServlet。在 spring boot 中不确定。

    试试这个。

        @Controller
        public class WelCont {
    
            @RequestMapping(value="/foldername_if_avaiable/welcome/", method=RequestMethod.GET )
            public ModelAndView wel(
                @RequestParam(value="id", required=false) String id,
                ModelMap model, 
                HttpSession session,
                HttpServletRequest req) throws Exception {
    
                model.addAttribute("test","testme");
    
                return new ModelAndView("/foldername_if_avaiable/welcome", model);
            }
        }
    

    reference

    【讨论】:

      猜你喜欢
      • 2016-06-15
      • 1970-01-01
      • 1970-01-01
      • 2011-05-02
      • 1970-01-01
      • 1970-01-01
      • 2016-02-16
      • 2011-07-26
      • 2020-01-26
      相关资源
      最近更新 更多