Spring Boot 1.4中JSP页面未解析:InternalViewResolver转发问题求助
Hey there, let's tackle this JSP rendering issue in your JHipster/Spring Boot 1.4 project. I know how frustrating it can be when you've spent days digging through solutions and still hit a wall—let's break down the key areas to check based on what you've described:
1. Double-Check JSP Dependencies
Spring Boot 1.4 has specific dependency requirements for JSP support. Make sure your pom.xml includes these (don't skip the JSTL dependency, it's critical for JSP functionality):
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency>
Note the provided scope for tomcat-embed-jasper—this works for the embedded Tomcat, but if you're deploying as a WAR to an external container, you may need to adjust this scope.
2. Validate Servlet Registration & View Resolver Configuration
You mentioned setting up a ServletContextInitializer and servletJsp-servlet.xml—let's make sure these are configured correctly:
- In your
WebConfigurer'sonStartupmethod, ensure the servlet maps to a path that doesn't conflict with JHipster's default routes, and points to your XML config:@Override public void onStartup(ServletContext servletContext) throws ServletException { XmlWebApplicationContext jspContext = new XmlWebApplicationContext(); jspContext.setConfigLocation("/WEB-INF/servletJsp-servlet.xml"); ServletRegistration.Dynamic jspServlet = servletContext.addServlet("jspServlet", new DispatcherServlet(jspContext)); jspServlet.setLoadOnStartup(1); jspServlet.addMapping("/jsp/*"); // Use a dedicated path to avoid clashes } - In
servletJsp-servlet.xml, you need an InternalResourceViewResolver (not just "InternalViewResolver"—that's a common mix-up) configured to find your JSPs:<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <!-- Match your actual JSP directory --> <property name="suffix" value=".jsp"/> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="order" value="1"/> <!-- Critical: Prioritize this resolver over JHipster's default --> </bean>
3. Avoid Conflicts with JHipster's Default View Resolver
JHipster uses Thymeleaf by default, which registers its own view resolver. Setting the order property (as above) ensures your JSP resolver gets priority for requests matching your servlet mapping. Without this, Spring might try to use Thymeleaf to resolve your JSP views, which will fail.
4. Confirm JSP File Placement & Controller Logic
- Make sure your JSP files are exactly in the directory specified by the
prefix(e.g.,WEB-INF/jsp/welcome.jspif prefix is/WEB-INF/jsp/). - When your controller returns a view name, use just the base name (like
welcomeinstead ofwelcome.jspor/WEB-INF/jsp/welcome)—the resolver will handle adding the prefix and suffix.
5. Debug the View Resolution Flow
Enable Spring MVC debug logging to see exactly what's happening when a request comes in. Add this to your application.properties:
logging.level.org.springframework.web.servlet=DEBUG
You'll get detailed logs showing which view resolvers Spring is trying, whether it finds your JSP file, and any errors that occur during resolution.
内容的提问来源于stack exchange,提问作者Xavier Marquis

