Say I have a struts.properties file with a defined value uploads.directory . How can I access that value from an Actioncontext programatically?
You can use getText("some.property.name") which return you the property value
http://struts.apache.org/maven/struts2-core/apidocs/com/opensymphony/xwork2/ActionSupport.html
Create ActionSupport Object and by using getText() method of ActionSupport class.
ActionSupport actionSupport = new ActionSupport();
actionSupport.getText("foo.bar");
Create a resources folder under src.
In the struts.xml file add a constant e.g., <constant name="struts.custom.i18n.resources" value="global"></constant>
Here global is the name of properties file.
Now you will be able to use the properties in the entire application.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- constant to define result path locations to project root directory -->
<!-- constant to define global resource bundle -->
<constant name="struts.custom.i18n.resources" value="global"></constant>
<package name="user" namespace="/" extends="struts-default">
<action name="home">
<result>/home.jsp</result>
</action>
<action name="welcome" class="com.waqar.struts2.actions.WelcomeAction">
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
The welcome.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><s:property value="getText('action.welcome.title')"/></title>
</head>
<body>
<s:property value="getText('action.welcome.username')"/>: <s:property value="username"/><br>
</body>
</html>
global.properties
action.welcome.username=waqar
In action class
System.out.println(getText("action.welcome.username"));
You need to put the my.properties file or my_locale.propeties file in the package that houses your action class.
You need to put the values in properties files other than struts.properties for examples ApplicationResources.properties or my.properties which needs to be in the classpath. struts.properties file is used to load struts specific properties for example struts.i18n.encoding=UTF-8 or struts.devMode = false etc.
The thing you need to do in struts.properties after you create the properties file for your customized messages is you have to add the following property in struts.properties file
struts.custom.i18n.resources=ApplicationResources
If you have more than one custom message property files then you need to add them by separating with comma for example:
struts.custom.i18n.resources=ApplicationResources,my
Then in your action classes you can access the property values by using getText('propertyName')
you can get value from message resource file like this:
public class MyAction extends ActionSupport {
public String getUserDetails() {
if("First Name".equals(getText("label.firstName"))) {
System.out.println("In if block");
}
}
}
you can also get more information, how to get values from .properties files in java class or jsp files.
for JSP:
<s:text name="label.firstName" />
and
<s:property value="getText('label.age')" />
for more information you can go through this link:
get info here
Related
I want to change the filenames of the PDF files in Magento 2.
The default names are not clear and I want invoices to be searchable when saved to a location on my pc.
Is it possible to change the filenames of the PDF-files in Magento 2 to a format like "invoice_1000000123.pdf"?
You should never edit core files. Seriously, don't.
Since /vendor/magento/module-sales/Controller/Adminhtml/Invoice/AbstractInvoice/PrintAction.php is an abstract class you have to use a plugin or preference in your module in order to override it.
What you need to achieve that:
The usual minimum files:
/Vendor/Module/composer.json, /Vendor/Module/registration.php, /Vendor/Module/etc/module.xml
In /Vendor/Module/etc/module.xml you should sequence Magento_Sales
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_Module" setup_version="1.0.0">
<sequence>
<module name="Magento_Sales"/>
</sequence>
</module>
</config>
Then you can either use a plugin or a preference in /Vendor/Module/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Sales\Controller\Adminhtml\Invoice\AbstractInvoice\PrintAction" type="Vendor\Module\Controller\Adminhtml\Invoice\AbstractInvoice\PrintAction"/>
</config>
Plugin would look something like this:
<type name="Magento\Sales\Controller\Adminhtml\Invoice\AbstractInvoice\PrintAction">
<plugin name="Vendor_Module::aroundPrintInvoice" type="Vendor\Module\Block\Class" sortOrder="0"/>
</type>
Now add a PrintAction.php to the path specified in your preference / plugin (Example for preference)
<?php
namespace Vendor\Module\Controller\Adminhtml\Invoice\AbstractInvoice;
class PrintAction extends \Magento\Sales\Controller\Adminhtml\Invoice\AbstractInvoice\PrintAction
{
/* Write code here */
}
You need override the invoice admin controller.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="\Magento\Sales\Controller\Adminhtml\Order\Invoice"
type="Vendor\Module\Controller\Adminhtml\Order\Invoice" />
</config>
In your custom module admin controller, You need to change the name of Pdf file.
public function execute()
{
$invoiceId = $this->getRequest()->getParam('invoice_id');
return $this->_fileFactory->create(
'invoice_13012020' . $invoiceId . '.pdf', //<== Change the pdf name here.
$pdf->render(),
DirectoryList::VAR_DIR,
'application/pdf'
);
...
}
Reference Link
Yes it is possible to change invoice pdf filename.
Please go to following path:
/vendor/magento/module-sales/Controller/Adminhtml/Invoice/AbstractInvoice/PrintAction.php
You can change filename from above file.
I am trying to add file upload to my Spring Webflog form processing. As far as the form enctype is not set to multipart/form-data, form submition works just fine. But after I added enctype="multipart/form-data" to my Spring form, this Exception occurs:
java.lang.IllegalStateException: A flow execution action URL can only be obtained in a RenderRequest or a ResourceRequest
at org.springframework.webflow.context.portlet.PortletExternalContext.getFlowExecutionUrl(PortletExternalContext.java:215)
at org.springframework.webflow.engine.impl.RequestControlContextImpl.getFlowExecutionUrl(RequestControlContextImpl.java:178)
at org.springframework.webflow.mvc.view.AbstractMvcView.render(AbstractMvcView.java:189)
at org.springframework.webflow.engine.ViewState.render(ViewState.java:293)
at org.springframework.webflow.engine.ViewState.refresh(ViewState.java:242)
at org.springframework.webflow.engine.ViewState.resume(ViewState.java:220)
at org.springframework.webflow.engine.Flow.resume(Flow.java:537)
at org.springframework.webflow.engine.impl.FlowExecutionImpl.resume(FlowExecutionImpl.java:259)
at org.springframework.webflow.executor.FlowExecutorImpl.resumeExecution(FlowExecutorImpl.java:169)
at org.springframework.webflow.mvc.portlet.FlowHandlerAdapter.handleAction(FlowHandlerAdapter.java:161)
at org.springframework.web.portlet.DispatcherPortlet.doActionService(DispatcherPortlet.java:670)
at org.springframework.web.portlet.FrameworkPortlet.processRequest(FrameworkPortlet.java:520)
at org.springframework.web.portlet.FrameworkPortlet.processAction(FrameworkPortlet.java:461)
at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:71)
I have added CommonsMultipartResolver to my spring context:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- Limit uploads to one byte smaller than the server is allowed to handle -->
<property name="maxUploadSize" value="100000" />
</bean>
and have commons-fileupload.jar in my pom.xml:
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
My JSP looks like this:
<portlet:actionURL var="processFormAction" >
<portlet:param name="execution" value="${flowExecutionKey}"/>
</portlet:actionURL>
<form:form action="${processFormAction}" modelAttribute="customerModel" enctype="multipart/form-data" method="post" >
<form:input path="firstName" cssClass="input-size-1 valid-required" />
<form:input path="lastName" cssClass="input-size-1 valid-required" />
<input name="avatar" id="avatar" type="file"/>
<input type="submit" name="_eventId_submit" id="send" value="Submit"/>
</form:form>
My flow.xml definition:
<view-state id="state1" model="customerModel">
...
<transition on="submit" to="submitFormActions"/>
</view-state>
<action-state id="submitFormActions">
<evaluate expression="portletAction.processForm(customerModel, flowRequestContext)" />
<transition on="success" to="state2"/>
<transition on="error" to="state1" />
</action-state>
The model object:
public class CustomerModel implements Serializable{
private String firstName;
private String lastName;
private MutlipartFile avatar;
...
//public getters and setters
}
Any thoughts what could be wrong? As I said, without enctype="multipart/form-data" the form processing works well.
Thanks
You are using org.springframework.web.multipart.commons.CommonsMultipartResolver which is not aware about the portlet context.
You need to change CommonsMultipartResolver to:
<bean id="portletMultipartResolver"
class="org.springframework.web.portlet.multipart.CommonsPortletMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>
Also, for this bean to be recognised by DispatcherPortlet, you need to define this bean id as mentioned above. The doc says:
Any configured PortletMultipartResolver bean must have the following id (or name): "portletMultipartResolver".
If you have defined your PortletMultipartResolver with any other name, then the DispatcherPortlet will not
find your PortletMultipartResolver, and consequently no multipart support will be in effect.
I'm using Enunciate on a multi module maven project. I use version 1.28 and I just use it for documentation purposes on SOAP Services.
This works just fine for all the Services.
The targetNamespace and endpointInterface has to be declared in the #WebService annotation and everything works fine. I got my zip with wsdl/wadl/xsd/html output.
All javadoc is recognized and published through the output files.
BUT...I would not write here if there is no but...
All data model files won't! I tried the following options:
<api-import pattern="package.model.**" />
<modules>
<spring-app disabled="true" />
<docs docsDir="/docs" title="Web Service API" copyright="ME" />
<!-- Disable all the client generation tools -->
<basic-app disabled="true" />
<c disabled="true" />
<csharp disabled="true" />
<java-client disabled="true" />
<jaxws-client disabled="true" />
<jaxws-ri disabled="true" />
<jaxws-support disabled="true" />
<jersey disabled="true" />
<obj-c disabled="true" />
<xml forceExampleJson="true" />
<jaxws disabled="true" />
<amf disabled="true" />
</modules>
module is not included in webarchive but declared as dependency:
<dependency>
<groupId>package.model</groupId>
<artifactId>model</artifactId>
<version>${project.parent.version}</version>
<scope>provided</scope>
</dependency>
The DTOs and ENUMS in the Data Model are normally provided with:
#XmlType(namespace = "https://package/DTO")
And Javadoc on class and attributes.
But I tried Javadoc on getters and setters too.
I even tried some xml annotation from the example implementation in my project:
#javax.xml.bind.annotation.XmlType(name = "socialGroup", namespace = "http://api.ifyouwannabecool.com/link")
#javax.xml.bind.annotation.XmlRootElement(name = "socialGroup", namespace = "http://api.ifyouwannabecool.com/link")
Without success. The javadoc won't be included in xsd/wsdl/html files as it does for the SOAP Services.
Do you have any idea?
If the classes are in a different Maven module, as you have declared in a dependency, you have to explicitly tell Enunciate to "import" them in order to get the JavaDoc included.
So let's pretend your model classes are in a package called "org.mycompany.widgets.model" and in a package called "org.mycompany.gadgets.model". Tell Enunciate to import those like this:
<enunciate ...>
...
<api-import pattern="org.mycompany.gadgets.model.**"/>
<api-import pattern="org.mycompany.widgets.model.**"/>
...
Please add #XmlRootElement on top of the class so that enunciate will make them appear in Data Model documentation.
Example :
#XmlRootElement
public class Foo{
....
}
I think the problem is <scope>provided</scope>. If the classes aren't present in the classpath, enunciate will not be able to find them. Change it to <scope>compile</scope>, along with the #XmlRootElement annotations and it should work.
If I have the following Docbook 5 XML:
<article xmlns="http://docbook.org/ns/docbook" xmlns:xl="http://www.w3.org/1999/xlink" version="5.0">
<title>Test</title>
<section>
<title>LS command</title>
<para xml:id="ls">
This command is a synonym for <command linkend="dir">DIR</command> command.
</para>
<para xml:id="dir">
This command is a synonym for <command linkend="ls">LS</command> command.
</para>
<para>
<application xl:href="http://www.gnu.org/software/emacs/">Emacs</application>
</para>
</section>
</article>
and I attempt to validate it against the Docbook 5 DTD downloaded from here with:
xmllint --noout --dtdvalid docbook.dtd test.xml
I get the following error:
test.xml:1: element article: validity error : No declaration for attribute xmlns:xl of element article
test.xml:11: element application: validity error : No declaration for attribute href of element application
However, if I change the xl namespace to xlink, like so:
<article xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink" version="5.0">
<title>Test</title>
<section>
<title>LS command</title>
<para xml:id="ls">
This command is a synonym for <command linkend="dir">DIR</command> command.
</para>
<para xml:id="dir">
This command is a synonym for <command linkend="ls">LS</command> command.
</para>
<para>
<application xlink:href="http://www.gnu.org/software/emacs/">Emacs</application>
</para>
</section>
</article>
Everything validates just fine. I got the xl namespace from the Docbook documentation here (and the example of using an article with an application that had a xref is straight from that documentation).
So why does xl fail when xlink succeeds?
The issue was actually that the DocBook 5.0 DTD specifically listed the xlink:href attribute, and any others were not valid.
I am trying to run this simple example on eclipse .As i run the server it is not showing any error .But in the browser it is showing 404 error page not found. I don't understand what is the error.I have used all the Jars in my WEB-INF/Lib folder too.
THE CODE SNIPPET is below:
CLASS HELLOWORLDACTION
package com.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.form.HelloWorldForm;
public class HelloWorldAction extends Action{
public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) throws Exception {
HelloWorldForm helloWorldForm = (HelloWorldForm) form;
helloWorldForm.setMessage("Hello World! Struts");
return mapping.findForward("success");}
}
CLASS HelloWorldForm
package com.form;
import org.apache.struts.action.ActionForm;
public class HelloWorldForm extends ActionForm{
String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="helloWorldForm"
type="com.form.HelloWorldForm"/>
</form-beans>
<action-mappings>
<action path="/helloWorld"
type="com.action.HelloWorldAction"
name="helloWorldForm">
<forward name="success" path="/HelloWorld.jsp"/>
</action>
</action-mappings>
</struts-config>
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Maven Struts Examples</display-name>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/struts-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
HelloWorld.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%#taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1><bean:write name="helloWorldForm" property="message" />
</h1>
</body>
</html>
It sounds like you are using the wrong url. Tomcat generally has a folder called webapps for applications that are deployed on that server. To figure out the configuration, in Eclipse, open up the Server tab, find your Tomcat server and double-click on it.
It should bring up a properties pane with two tabs at the bottom Overview and Modules. Click on Modules and you should see the path to your app. For me, it's in the form of: /webapps/<project name>
Your full url would be localhost:8080/webapps/<project name>/anystringhere.do since you map anything in your app (localhost:8080/webapps/<project name>/) that ends in .do to the action servlet.