- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试创建 FreeMarker 和 Spring 的非常简单的集成。但是,当我运行示例时,出现异常:
GRAVE: Servlet.service() for servlet MyServlet threw exception
javax.servlet.ServletException: Could not resolve view with name 'Home' in servlet with name 'MyServlet'
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1162)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)
我不知道我的配置有什么问题:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.jverstry.Configuration</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file></welcome-file>
</welcome-file-list>
</web-app>
网页配置
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "com.jverstry")
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver getViewResolver() {
FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
resolver.setCache(true);
resolver.setPrefix("WEB-INF/pages/");
resolver.setSuffix(".ftl");
return resolver;
}
@Bean
public FreeMarkerConfigurer getFreemarkerConfig() {
FreeMarkerConfigurer result = new FreeMarkerConfigurer();
result.setTemplateLoaderPath("WEB-INF/pages/");
return result;
}
}
Controller
@Controller
public class MyController {
@RequestMapping(value = "/")
public String home(@ModelAttribute("model") ModelMap model) {
model.addAttribute("MsTime", System.currentTimeMillis());
return "Home";
}
}
P.S.:当然,在WEB-INF/pages/
中有一个Home.ftl
文件。
最佳答案
找了很多,不设置前缀解决了这个问题:
@Bean
public ViewResolver getViewResolver() {
FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
resolver.setCache(false);
// resolver.setPrefix("");
resolver.setSuffix(".ftl");
return resolver;
}
关于java - 带有 FreeMarker : Could not resolve view with name 'Home' in servlet with name 'MyServlet' 的 Spring ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13085693/
我有一个 servlet,myservlet,在 servlet-mapping 下的 web.xml 文件中,我已经映射到 url-pattern,index.html。请求 http://mydo
这个问题在这里已经有了答案: Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
这个问题在这里已经有了答案: java.lang.IllegalArgumentException: The servlets named [X] and [Y] are both mapped t
我创建了一个 jira 7.6.1 插件(使用 atlas-create-jira-plugin)和一个 servlet 模块(使用 atlas-create-jira-plugin-module )
我的index.jsp页面的代码是 Insert title here Welcome to the jsp page
我正在尝试自学J2EE。从Servlet开始..所以我下载了J2ee的eclipse.,前几天tomcat运行得很好...但是现在,它没有运行,事实上它给出了错误,即Server Tomcat v8.
我是使用 servlet 的新手。 我正在创建一个 servlet,但在运行之后出现错误 谁能帮我解决这个问题。 错误 Type Exception Report Message Class [com
我是使用 servlet 的新手。 我正在创建一个 servlet,但在运行之后出现错误 谁能帮我解决这个问题。 错误 Type Exception Report Message Class [com
MyServlet.java class MyServlet extends HttpServlet{ private static final long serialVersion
我正在尝试创建 FreeMarker 和 Spring 的非常简单的集成。但是,当我运行示例时,出现异常: GRAVE: Servlet.service() for servlet MyServlet
我是一名优秀的程序员,十分优秀!