gpt4 book ai didi

java - Struts 2 与 Thymeleaf

转载 作者:行者123 更新时间:2023-12-02 04:34:47 28 4
gpt4 key购买 nike

我想在我的网络应用程序中使用 Thymeleaf 作为模板引擎。我在Google上搜索了如何使用struts2配置Themeleaf,并找到了一些解决方案 plugin 。我已在我的应用程序中配置了插件,但无法使用插件呈现页面。相反,我收到错误:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/WEB-INF/templates/hello.html", template might not exist or might not be accessible by any of the configured Template Resolvers
org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:246)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:955)
com.struts.custom.resulttype.plugin.ThemeleafResult.execute(ThemeleafResult.java:51)
com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:374)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:278)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:192)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:510)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)

这是DefaultTemplateEngineProvider.java的实现

public class DefaultTemplateEngineProvider implements TemplateEngineProvider {
// HTML5 is the future!
private String templateMode = "HTML5";
private String characterEncoding = "UTF-8";
// This will convert "home" to "/WEB-INF/templates/home.html"
private String prefix = "/WEB-INF/templates/";
private String suffix = ".html";
private boolean cacheable = true;

// Default template cache TTL to 1 hour. If not set, entries would live in
// cache until expelled by LRU.
private Long cacheTtlMillis = 3600000L;

private TemplateEngine templateEngine;

/**
* Configure settings from the struts.xml or struts.properties, using sensible
* defaults if values are not provided.
*/
public void configure() {
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setTemplateMode(templateMode);
templateResolver.setCharacterEncoding(characterEncoding);
templateResolver.setPrefix(prefix);
templateResolver.setSuffix(suffix);
templateResolver.setCacheable(cacheable);
templateResolver.setCacheTTLMs(cacheTtlMillis);

templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
templateEngine.setMessageResolver(new StrutsMessageResolver());
}


public TemplateEngine get() {
if (templateEngine == null) {
configure();
}

return templateEngine;
}

@Inject(value = "struts.thymeleaf.templateMode", required = false)
public void setTemplateMode(String templateMode) {
this.templateMode = templateMode;
}

@Inject(value = "struts.thymeleaf.encoding", required = false)
public void setCharacterEncoding(String characterEncoding) {
this.characterEncoding = characterEncoding;
}

@Inject(value = "struts.thymeleaf.prefix", required = false)
public void setPrefix(String prefix) {
this.prefix = prefix;
}

@Inject(value = "struts.thymeleaf.suffix", required = false)
public void setSuffix(String suffix) {
this.suffix = suffix;
}

@Inject(value = "struts.thymeleaf.cacheable", required = false)
public void setCacheable(String cacheable) {
this.cacheable = Boolean.parseBoolean(cacheable);
}

@Inject(value = "struts.thymeleaf.cacheTtlMillis", required = false)
public void setCacheTtlMillis(String cacheTtlMillis) {
this.cacheTtlMillis = Long.parseLong(cacheTtlMillis);
}
}

我已经编写了自定义结果集并配置了 Thymeleaf,如官方网站上所述。但得到同样的错误。

我的自定义结果类型是

public class ThymeleafResult implements Result {
private String defaultEncoding = "UTF-8";
private TemplateEngineProvider templateEngineProvider;
private String templateName;

/**
* The result parameter name to set the name of the template to.
* <p/>
* IMPORTANT! Struts2 will look for this field reflectively to determine which
* parameter is the default. This allows us to have a simplified result
* configuration. Don't remove it!
*/
public static final String DEFAULT_PARAM = "templateName";

public ThymeleafResult() {
}

public ThymeleafResult(String templateName) {
this.templateName = templateName;
}


public void execute(ActionInvocation actionInvocation) throws Exception {
TemplateEngine templateEngine = templateEngineProvider.get();

HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
ServletContext servletContext = ServletActionContext.getServletContext();

Object action = actionInvocation.getAction();

StrutsContext context = new StrutsContext(request, response, servletContext, action);
response.setContentType("text/html");
response.setCharacterEncoding(defaultEncoding);
System.out.println(templateName);
templateEngine.process(templateName, context, response.getWriter());
}

@Inject(StrutsConstants.STRUTS_I18N_ENCODING)
public void setDefaultEncoding(String defaultEncoding) {
this.defaultEncoding = defaultEncoding;
}

@Inject
public void setTemplateEngineProvider(TemplateEngineProvider templateEngineProvider) {
this.templateEngineProvider = templateEngineProvider;
}

public void setTemplateName(String templateName) {
this.templateName = templateName;
}

struts.xml是

<struts>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.multipart.maxSize" value="1000000000" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<bean type="org.codework.struts.plugins.thymeleaf.spi.TemplateEngineProvider" name="default" class="org.codework.struts.plugins.thymeleaf.spi.DefaultTemplateEngineProvider" />

<package name="default" namespace="/" extends="struts-default">
<result-types>
<result-type name="thymeleaf" class="org.codework.struts.plugins.thymeleaf.ThymeleafResult"/>
</result-types>
<default-action-ref name="home"></default-action-ref>
<action name="home" class="com.demo.example.Welcome">
<result name="success" type="thymeleaf">/WEB-INF/templates/hello.html</result>
</action>
</package>
</struts>

最佳答案

我找到了解决方案:我在 struts.xml 中进行了错误的映射。

我刚刚更换

<result name="success" type="thymeleaf">/WEB-INF/templates/hello.html</result> 

<result name="success" type="thymeleaf">hello</result> 

并且成功了。

关于java - Struts 2 与 Thymeleaf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30982665/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com