在我分配的使用Struts2的项目中,我需要做以下事情,但我不熟悉使用Struts2。
.properties
文件。.properties
文件中。我已经能够更改一些标题,首先将Struts2与我的.properties
文件相关联,然后执行answer。
Content.properties
login.logo=/resources/media/login.png
login.title=Login
现在,我的JSP文件仅用作HTML文件,它的外观如下:
的login.jsp
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="sj" uri="/struts-jquery-tags"%>
<%@taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>
<%@taglib prefix="sb" uri="/struts-bootstrap-tags"%>
<%@taglib prefix="decorator" uri="sitemesh-decorator"%>
<%
response.setHeader("Cache-Control", "no-cache"); //HTTP 1.1
response.setHeader("Pragma", "no-cache"); //HTTP 1.0
response.setDateHeader("Expires", -1); //evita o caching no servidor proxy
%>
<!DOCTYPE>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta charset="UTF-8" />
<title><s:text name="login.title" /></title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<link rel="shortcut icon" href='<s:url value="/resources/media/favicon_seg.ico"/>' />
<!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<sj:head locale="es" jqueryui="true" jquerytheme="ui-lightness" compressed="true" defaultIndicator="ajax_loading" />
<sb:head />
<link rel="stylesheet" href='<s:url value="/resources/css/login.css"/>' />
</head>
<body>
<div class="panel-footer ">
<img height="40" src="<s:url value="/resources/media/login.png"/>" class="pagelogo" alt="Logo" /> My Company
</div>
</body>
</html>
但是,如果我想使用属性文件在上面的文件中更改此部分,如下所示:
<img height="40" src="<s:url value="${login.logo}"/>" class="pagelogo" alt="Logo" /> My Company
有时我会正确显示页面,有时不会,但刷新后(F5)它会再次显示或不显示。如果没有显示,我会收到以下错误:
SEVERE: Servlet.service() for servlet [jsp] threw exception
org.apache.jasper.JasperException: /WEB-INF/content/login.jsp (line: [87], column: [28]) According to TLD or attribute directive in tag file, attribute [value] does not accept any expressions
at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:291)
at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:114)
at org.apache.jasper.compiler.Validator$ValidateVisitor.checkXmlAttributes(Validator.java:1241)
at org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:879)
at org.apache.jasper.compiler.Node$CustomTag.accept(Node.java:1545)
at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2392)
at org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2444)
at org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2450)
at org.apache.jasper.compiler.Node$Root.accept(Node.java:471)
at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2392)
at org.apache.jasper.compiler.Validator.validateExDirectives(Validator.java:1853)
at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:221)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:374)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:351)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:335)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:601)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:368)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:59)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:728)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:590)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:524)
at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:895)
at org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.java:648)
at org.apache.struts2.dispatcher.ServletDispatcherResult.doExecute(ServletDispatcherResult.java:132)
at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:191)
at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:372)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:276)
at org.apache.struts2.interceptor.CookieProviderInterceptor.intercept(CookieProviderInterceptor.java:75)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at org.apache.struts2.interceptor.CookieInterceptor.intercept(CookieInterceptor.java:258)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at org.apache.struts2.interceptor.CookieInterceptor.intercept(CookieInterceptor.java:258)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:76)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:229)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:253)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
at org.apache.struts2.components.ActionComponent.executeAction(ActionComponent.java:288)
at org.apache.struts2.components.ActionComponent.end(ActionComponent.java:172)
at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
at org.apache.jsp.index_jsp._jspx_meth_s_005faction_005f0(index_jsp.java:168)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:127)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
at org.apache.catalina.core.ApplicationFilterC2017-12-11 11:04:52,566 ERROR (org.apache.struts2.components.ActionComponent) - Could not execute action: //login
hain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
什么是获取图像路径并将其显示在JSP中的正确方法,而不会有时会出现上述错误?
我发现了一些与此错误相关的问题,但他们使用的是JSTL,我不是。
我在写这个问题的时候发现了另一篇文章:JSP2 expression in Struts2 form input他们建议使用OGNL,我从中读到的是表格:
<s:url ...>
如果我错了,请纠正我,因为这些是我的新概念
这是帖子提取
默认情况下,您无法将JSP EL表达式传递给Struts2标记。这样做是为了安全起见。如果您愿意,可以复制struts-tags.tld,并为所有标记设置为true。但是,您应该了解允许标记接受JSP EL和OGNL表达式所涉及的安全漏洞。
在调用标记处理程序之前评估JSP EL,之后评估OGNL(在标记内)。因此,如果$ {retPage}被评估为OGNL表达式,那么标签可能会结束您做不到的事情。
那么,在这种情况下,可以有任何解决方法来做我想做的事情(从我的属性文件中获取图像URL并使用OGNL或EL显示它?
我也试过这个答案的解决方案:https://stackoverflow.com/a/21673153/2180785
建议将s:url
更改为c:url
,在这种情况下,它不会生成空白页(因为问题提到了它,并且在重新加载页面时也会发生这种情况),但是导致broken-image
图像以及一些html,如下所示:
如果需要,我可以提供所需的任何额外信息,因为我认为这是必需的,但正如我所说,我是这些技术的新手,因此不知道这个问题是否需要其他任何东西才能完成或足够明确回答。
答案 0 :(得分:1)
我一直在寻找我的问题的答案,并找到了以下问题:How to create dynamic images with struts2?
其中包含accepted answer中我的问题的答案:
使用
<img src=<s:text name="genTP" /> />
还有另一种简单的方法。但我觉得很奇怪。
在我的案例中,我们将其转化为:
<img height="40" src="<s:text name="login.logo"/>" class="pagelogo" alt="Logo" /> Login
让我的content.properties
文件以这种方式改变了图像路径:
login.logo=/oneFolderBefore/resources/media/login.png
login.title=Login
通过这种方式,我能够正确显示我的属性文件中的图像,避免了上述错误。