I have some difficulties to call a JNDI variable to my jsp Page.
Context.xml :
<!-- Environnement de l'application -->
<Environment name="app/env" override="false" type="java.lang.String" value="Développement" />
conf-view-spring.xml
<jee:jndi-lookup id="jndiLookEnv" jndi-name="app/env"
expected-type="java.lang.String" />
footer.jsp
<div class="panel-footer footerstick clearfix">
<div class="pull-right">
<strong><c:out value="${jndiLookEnv}"></c:out></strong>
</div>
</div>
I have no error but my jndiLookEnv is empty,
What's wrong in this case?
Thank you.
Beans in the context aren't accessible by the JSP (or at least not in the way you are trying to). You would need a scriptlet to get access to the ApplicationContext and do a getBean to retrieve the value. Using ${jndiLookEnv} isn't going to work.
To make it available for easy use in the JSP you need to add a ServletContextAttributeExporter to expose it.
<bean class="org.springframework.web.context.suppor.ServletContextAttributeExporter">
<property name="attributes">
<map>
<entry key="env" value-ref="jndiLookEnv" />
</map>
</properties>
</bean>
Now you can use ${env} to reference to value.
Now you can improve on this and remove the JNDI lookup al together (assuming that you are on a recent spring version).
<bean class="org.springframework.web.context.suppor.ServletContextAttributeExporter">
<property name="attributes">
<map>
<entry key="env" value="#{environment['app/env']}" />
</map>
</properties>
</bean>
The Environment abstraction will do a lookup for the property in various locations, one of them being JNDI (you could override the app/env property using properties or a system property for instance.
Related
Currently in my data context XML file, it's looking up values to substitute from a application.properties file:
<bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" lazy-init="default">
<property name="location" value="classpath:application.properties" />
</bean>
<bean id="appleDataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="url" value="${apple.url}" />
</bean>
I'd like to change this from being looked up from the application.properties file, to being read out of a Properties/Map object.
This is because the configuration could come from more than one place (i.e. not just the classpath), and this logic is already implemented in my application.
I've seen MapPropertySource but I think while that can be done when the bean is configured in Java, I'm not sure this logic can be implemented when working with the XML file alone?
After upgrading to Spring 4.3.3.RELEASE i get the error:
Request method 'POST' not supported
My application is a basic template and the home view is rendered via
<mvc:view-controller path="/" view-name="home.view"/>
It works fine on Spring 4.2.8.
Any hint to solve the problem?
We ran into the same problem. It turns out that, at some point, the ParameterizableViewController was changed to only support GET and HEAD requests.
We resolved this by replacing the definition with something like this:
<bean id="homeController" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="supportedMethods" value="GET,POST,PUT,DELETE" />
<property name="viewName" value="home.view" />
</bean>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<map>
<entry key="/" value-ref="homeController"/>
</map>
</property>
</bean>
Essentially, this allows you to create a ParameterizableViewController with whatever supported HTTP methods you wish. The second bean creates the mapping so that the path "/" resolves to the defined controller.
ParameterizableViewController default supported methods are GET,HEAD we are check it with the following code snippet.
ParameterizableViewController pvc=new ParameterizableViewController();
String[] str=pvc.getSupportedMethods();
for(String x:str) {
System.out.println(x);
}
in order to add POST or any HTTP method, we need to add this XML tag in our bean tag.
<bean id="testUrl"
class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="supportedMethods" value="GET,POST,PUT,DELETE" />
<property name="viewName" value="success" />
</bean>
I am new to spring. Admins of my spring based web app want to configure settings from the web interface, so users can authenticate against LDAP server with their company username and password.
Change in LDAP settings should be possible without restarting the application. This might happen during a 'migration' or whatever reason. I have a couple beans, which need to be refreshed after the admin saves new settings for the LDAP server:
<bean id="ldapServer" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
<constructor-arg>
<list>
<value>${ldap.url1}</value>
...
</list>
</constructor-arg>
<constructor-arg value="${ldap.basedn}"</constructor-arg>
<property name="referral" value="${ldap.referral}" />
<property name="baseEnvironmentProperties">...</property>
<property name="userDn" value="${ldap.username}" />
<property name="password" value="${ldap.password}" />
</bean>
I am using Springframework 3.1.2. The problem is, there are constructor arguments, which I want to change and not affect other running jobs. I tried playing with Scoped proxy, but not to much success yet:
<bean id="ldapServer" scope="prototype" ...>
<aop:scoped-proxy/>
I was successful though to get ldapServer to reinstantiate, when using prototype scope by running this piece of code:
#Controller
public class LDAPSettingsController implements ApplicationContextAware {
public ModelAndView handleRequest(...) {
DefaultSpringSecurityContextSource ldap;
ldap = context.getParentBeanFactor().getBean("ldapServer");
System.out.println(ldap.hashCode());
return new ModelAndView(new RedirectView('login.jsp'));
}
...
}
Are scopes and proxies here the way to go, or is the another mechanism in Spring to reflect configuration changes into a running program instance?
UPDATE: Clear up the question.
UPDATE: The root problem with the AOP proxies was following root exception:
java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
What worked was adding proxy-target-class="false" attribute to the <aop:scoped-proxy/> tag. I created a new scope, which works better than prototype - It destroys beans on settings update. Now I have this in my beans.xml:
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="ldap">
<ref bean="ldapScope" />
</entry>
</map>
</property>
</bean>
<bean id="ldapScope" class="com.myapp.SettingsScope" />
<bean id="ldapServer" scope="ldap" ...>
<aop:scoped-proxy proxy-target-class="false"/>
<constructor-args>
<list><value>${ldap.url1}</value> .. </list>
</constructor-args>
...
</bean>
I also have a controller for LDAP settings into which I inject ldapScope and I call a method which destroys current life-cycle objects and starts a new life-cycle every time, user presses the apply button.
PS: Not sure if I handle the life-cycle "re-start" in the right way - people my way to look for auto-start beans and start them after such event happens (i.e.: Setting -> Apply)
I've a properties file like this :
firstproperty=1,2,3,4
secondproperty=47,998,120
thirdproperty=54
My properties file is well defined in my Spring configuration as property for my PropertyPlaceHolderConfigurer bean.
I want to load values in a
HashMap<String, ArrayList<String>>
like this :
<util:map id="properties" map-class="java.util.HashMap">
<entry key="first" value="${firstproperty}" />
<entry key="second" value="${secondproperty}" />
<entry key="three" value="${thirdproperty}" />
</util:map>
The problem is that for each entry, multiple values separated by commas count as one value. I tried to configure value-type of my util-map to an ArrayList but it was unsuccessful. Any idea ?
P.S : I use Spring 3.2.
I searched Spring EL in config file, maybe this is what you want:
<bean id="taxCalculator" class="org.spring.samples.TaxCalculator">
<property name="defaultLocale" value="#{ systemProperties['user.region'] }.split(',')"/>
<!-- other properties -->
I am not sure about the location of split method, you can try yourself to find the correct way. For more details, please refer:http://docs.spring.io/spring/docs/3.0.x/reference/expressions.html
I am using spring webflow and I have registered all the flow xmls in the webflow.xml like this
<!-- The Flow handler adapter, to handle flows request recieved by the dispatcher servlet -->
<bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
<property name="flowExecutor" ref="flowExecutor"/>
</bean>
<flow:flow-registry id="myflowRegistry" flow-builder-services="flowBuilderServices" >
<!-- all xml files in base path and subfolders -->
<flow:flow-location path="/WEB-INF/flows/payslips.xml" />
<flow:flow-location path="/WEB-INF/flows/admissions.xml" />
<flow:flow-location id="cash-advance" path="/WEB-INF/flows/cashadvance.xml"/>
<flow:flow-location path="/WEB-INF/flows/services.xml" />
<flow:flow-location path="/WEB-INF/flows/undergradadm.xml" />
</flow:flow-registry>
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
<property name="order" value="0" />
<property name="flowRegistry" ref="myflowRegistry" />
</bean>
Now when I tr to access any of these pages with payslips.go or cash-advance.go they dont work and give me :
Error 500: Request processing failed; nested exception is java.lang.StringIndexOutOfBoundsException: String index out of range: 1
I am very new to webflow and It seems logical that it should work. The views in the spring MVC that are not part of the webflow or dont have any xmls defined under flow work perfectly fine because it has nothing to do with webflow. but these pages which have a flow defined. I dont think mozilla has anything to do with it.
On some other machine, these are working fine. there must be something with my own setup that its not letting it work.
May be this will help as well
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.substring(String.java:1060)
at org.springframework.webflow.context.servlet.DefaultFlowUrlHandler.getFlowId(DefaultFlowUrlHandler.java:83)
at org.springframework.webflow.mvc.servlet.FlowHandlerMapping.getHandlerInternal(FlowHandlerMapping.java:92)
at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:184)
at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1057)
Thanks
This is how I am configuring SWF:
<!--
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SPRING WEB FLOW'S CONFIGURATION
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-->
<!-- Creates a flow executor in Spring, responsible for creating and executing flows -->
<flow:flow-executor id="flowExecutor" flow-registry="flowRegistry" />
<!-- Load flow definitions and make them available to the flow executor -->
<flow:flow-registry id="flowRegistry">
<flow:flow-location id="process-flow" path="/process/flows/process-flow.xml" />
</flow:flow-registry>
<!-- The FlowHandlerMapping helps DispatcherServlet to knowing that it should send flow requests to Spring Web Flow -->
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
<property name="flowRegistry" ref="flowRegistry" />
</bean>
<!-- The FlowHandlerAdapter is equivalent to a Spring MVC controller in that it handles requests coming in for a flow and processes those requests -->
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
<property name="flowExecutor" ref="flowExecutor" />
</bean>
My flow is in the root of my web folder, but you can also use the WEB-INF dir; this way, the path should be "/WEB-INF/foo/bar.xml/".
I hope it helps, regards.