We have created a Spring mvc portlet with given application context file:
<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
</bean>
</beans>
On deploying the portlet,the below error message is thrown:
Caused by: org.xml.sax.SAXParseException; lineNumber: 6; columnNumber:
64; cvc-elt.1: Cannot find the declaration of element 'beans'. at
com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
at
com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
at
com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:396)
at
com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)
at
com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:284)
Caused by:
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:
Line 6 in XML document from PortletContext resource
[/WEB-INF/spring/portletpreferences-portlet.xml] is invalid; nested
exception is org.xml.sax.SAXParseException; lineNumber: 6;
columnNumber: 64; cvc-elt.1: Cannot find the declaration of element
'beans'.
The respective spring jars are available to the project as dependencies as shown below:
I have referred all available similar questions to this issue and tried the following:
Adding doctype tag
Removing xsd version
Using an empty elements with beans tag
Are there still any missing dependecies or errors in xml in context xml file.
Update: I have updated both spring beans and spring context jars to same version and also successfullly validated the xml file with xsd definition,but am still getting the same error post deployment.
I'm using Elastic Search for my project activities in which I communicated to the backend ES cluster through the spring utility
spring-data-elastic-search
Following are the spring-repository descriptions for the webapp.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<elasticsearch:transport-client id="client" cluster-nodes="localhost:9300" />
<bean name="elasticsearchTemplate"
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client" />
</bean>
<elasticsearch:repositories
base-package="com.customer.repositories" />
</beans>
Here I've specified the cluster node configuration as cluster-nodes="localhost:9300" and it is working fine with my local testing.
In production server we've a full functional cluster setup with host IP say (192.xx.xx.xx). So my problem is that, we have specified the cluster host in a yml file in /etc/project/es.yml file in the production server. So I need to tweak my application to take the cluster configuration from this custom location.
Since the above spring-repository xml initialized by the spring container, we are unable to override the behaviour. Is there any way to achieve it with spring-data-elastic-search ?
Finally I resolved my issue and sharing it here, so that it may be useful to some one else.
Changed YML idea to property file (es.props)
spring repository descriptions should be as follows
<?xml version="1.0" encoding="UTF-8"?>co
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch
http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:property-placeholder location="file:/etc/project/es.props" />
<elasticsearch:transport-client id="client" cluster-nodes="${es-host}:9300""/>
<bean name="elasticsearchTemplate"
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client" />
</bean>
<elasticsearch:repositories
base-package="com.customer.repositories" />
</beans>
Which used Spring PropertySourcePlaceHolder modification mechanism in 3.1+.
So it will look up for es.host in /etc/project/es.props. Local testers can override this property by starting the server with -Des.host=custom-cluser-host
Actually Mohsin(Spring-data-elastic-search developer) gave hints to arrive at this solution. Thanks Mohsin.
I have Quartz 2.2.1 and Spring 3.2.2. app on Eclipse Juno
This is my bean configuration:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Spring Quartz -->
<bean id="checkAndRouteDocumentsTask" class="net.tce.task.support.CheckAndRouteDocumentsTask" />
<bean name="checkAndRouteDocumentsJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="net.tce.task.support.CheckAndRouteDocumentsJob" />
<property name="jobDataAsMap">
<map>
<entry key="checkAndRouteDocumentsTask" value-ref="checkAndRouteDocumentsTask" />
</map>
</property>
<property name="durability" value="true" />
</bean>
<!-- Simple Trigger, run every 30 seconds -->
<bean id="checkAndRouteDocumentsTaskTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="checkAndRouteDocumentsJob" />
<property name="repeatInterval" value="30000" />
<property name="startDelay" value="15000" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="checkAndRouteDocumentsJob" />
</list>
</property>
<property name="triggers">
<list>
<ref bean="checkAndRouteDocumentsTaskTrigger" />
</list>
</property>
</bean>
My mvc spring servlet config:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
</bean>
<mvc:annotation-driven />
<context:annotation-config />
<context:component-scan base-package="net.tce" />
<import resource="spring-quartz.xml"/>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>OperationalTCE</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
My problem is that always when startup my application, Quartz creates two jobs at the same time. My job must be execute every 30 seconds:
INFO: Starting TASK on Mon Nov 04 15:36:46 CST 2013...
INFO: Starting TASK on Mon Nov 04 15:36:46 CST 2013...
INFO: Starting TASK on Mon Nov 04 15:37:16 CST 2013...
INFO: Starting TASK on Mon Nov 04 15:37:16 CST 2013...
INFO: Starting TASK on Mon Nov 04 15:37:46 CST 2013...
INFO: Starting TASK on Mon Nov 04 15:37:46 CST 2013...
Thanks for your help.
Your ContextLoaderListener and DispatcherServlet are both loading the mvc-dispatcher-servlet.xml. Basically duplicating all your beans resulting in 2 executions as that is also duplicated.
Split your configuration into one that is being loaded by the ContextLoaderListener (containing your services, dao, timers etc.) and one loaded by the DispatcherServlet (containing only web related beans controllers, view resovlers etc.).
Or ditch the ContextLoaderListener altogether and only use the DispatcherServlet to load everything.
Make sure you are not loading both the files in your web.xml file for your project. If you load the spring-quartz.xml file separately, and then load the servlet-config.xml file separately that "imports" the spring-quartz.xml file, then you are loading the file twice which would result in 2 instances of your scheduler. The easy fix would be to either (1) ensure you are not loading spring-quartz.xml in your web.xml file OR (2) remove the import statement in your other xml file.
Updated: Thanks for showing us the web.xml to rule that out. After closer inspection of your xml files, you are setting up the SchedulerFactoryBean with both the job details and the trigger. This is your problem. The job details are included as part of your trigger, so putting them in again causes it to be scheduled twice. Please read the documentation on these two links about the setJobDetails() method:
http://docs.spring.io/spring/docs/2.5.6/api/org/springframework/scheduling/quartz/SchedulerFactoryBean.html
http://docs.spring.io/spring/docs/2.5.6/api/org/springframework/scheduling/quartz/SchedulerAccessor.html#setJobDetails%28org.quartz.JobDetail[]%29
http://docs.spring.io/spring/docs/2.5.6/api/org/springframework/scheduling/quartz/SchedulerAccessor.html#setTriggers%28org.quartz.Trigger[]%29
Excerpt of important info:
setJobDetails
public void setJobDetails(JobDetail[] jobDetails)
Register a list of JobDetail objects with the Scheduler that this FactoryBean creates, to be referenced by Triggers.
This is not necessary when a Trigger determines the JobDetail itself: In this case, the JobDetail will be implicitly registered in combination with the Trigger.
The solution would be to remove the jobDetails from the SchedulerFactoryBean like this:
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="checkAndRouteDocumentsTaskTrigger" />
</list>
</property>
</bean>
I'm developing a software based upon Spring and ICEFaces.
I have a file at <project directory>/src/main/webapp/WEB-INF/views/<filename>.xhtml, that is correctly reached using the following URL: http://<hostname>/<projectname>/<filename>.xhtml
The file contains a <h:form id="formId"> which is rendered as a <form action="/<projectname>/WEB-INF/views/<filename>.xhtml" [.. some other stuff ..]>
It means that, when I click on the input submit contained in the form, the browser tries to open the URL http://<hostname>/<projectname>/WEB-INF/views/<filename>.xhtml and, as I said in the title, it shows an error 404 page.
I'd like that the file .xhtml could be reached using the "longer" URL, too. I'm pretty sure that I'm currently unable to achieve that due to a configuration error.
This is my web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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"
version="2.5">
<web-app>
<display-name>SIGLO</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
This is the applicationContext.xml referenced in the previous file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" >
<context:component-scan base-package="com.infoone.siglo" />
<!--
map all requests to /resources/** to the container default servlet
(ie, don't let Spring handle them)
-->
<bean id="defaultServletHttpRequestHandler" class="org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler" />
<bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
<property name="urlMap" >
<map>
<entry key="/resources/**" value-ref="defaultServletHttpRequestHandler" />
</map>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" />
<mvc:annotation-driven />
<!-- JSF for representation layer. All JSF files under /WEB-INF/views directory -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver" >
<property name="cache" value="false" />
<property name="viewClass" value="org.springframework.faces.mvc.JsfView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean name="icefacesResourceHandler" class="org.springframework.faces.webflow.JsfResourceRequestHandler" />
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="order" value="0" />
<property name="mappings">
<value>
/javax.faces.resource/**=icefacesResourceHandler
</value>
</property>
</bean>
</beans>
And, in the end, this is my faces-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0" 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-facesconfig_2_0.xsd">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
<locale-config></locale-config>
<resource-bundle>
<base-name>MessageResources</base-name>
<var>msg</var>
</resource-bundle>
</application>
</faces-config>
Let me point out that this configuration still does not allow me to open successfully the shorter URL. Indeed, I have to create a proper controller or, better, a proper #RequestMapping inside a #Controller:
#RequestMapping(value = "<filename>", method = RequestMethod.GET)
public String creaBlocco()
{
return "<filename>";
}
#RequestMapping(value = "<filename>", method = RequestMethod.POST)
public String creaBlocco([.. parameters ..]) {
[.. stuff ..]
return "<filename>";
}
Yes, the value of the #RequestMapping is "<filename>", without the .xhtml extension. I already made sure, by trial and error, that such mapping is necessary for the GET to be successful. On the other hand, I realize that such configuration is really fragile. What should I change in my config files, in order to make <filename>.xhtml reachable using also the longer URL?
Thanks in advance for your attention.
After some research, I came up with the conclusion that I wasn't using those technologies in the way they're meant to be used. If you're facing my same problem, within the same context, you are probably making my same error, therefore you have to slightly modify your application architecture to solve it. Indeed, it seems that using JSF/ICEFaces and Spring MVC in conjunction with Spring WebFlow is the most comfortable and straightforward approach.
I'll try to explain the rationale.
Your site/user home page is filled up with links to the use cases (or, better, flows) that can be activated with his/her privileges. Those links look like http://<page-url>?<flow-id>. A flow in Spring WebFlow is defined as a graph of states, within an xml file. The transitions between states are labeled by strings, named actions, or conditions. Some states are view-state s, meaning that there's something to show to the user when such a state is reached. Therefore, for each view-state named <state-id>, there shall be an .xhtml file named <state-id>.xhtml.
The buttons provided by JSF and ICEFaces will always be rendered as <input type="submit" [..]> and the action of the containing form will always point to the same URL as the containing page (i.e. http://<page-url>?<flow-id>). That's a fact.
The difference is that, in this case, such URL is not backed by a real file (like in my question) but, if the app is properly configured, a flow-handler will answer and manage all the states and the transitions defined in the xml file, choosing the proper view to show.
Beside that, the buttons provided by JSF and ICEFaces have an action attribute. As you can imagine, they correspond to the actions defined in the flow definition file: therefore, a click on a button with a certain value of the action attribute will trigger the transition from the current state labeled exactly with that string.
Speaking more concretely, if you click on the button, the browser sends a POST request to http://<page-url>?<flow-id> whose body contains all the necessary parameters. The flow handler receives that POST request and computes the next state, possibly choosing which view has to be shown to the user.
If you're still reading this answer, I guess you need a concrete example to refer to. I'll provide you with the one I used to learn all the things I wrote here: http://wiki.icesoft.org/display/ICE/Spring+Web+Flow+2.3.1 It's an awesome starting point, because it's easy but exhaustive.
Can anyone please tell me how do we determine what namespace is required when we write down spring-servlet.xml from scratch?
For example I have the following spring-servlet.xml:
<beans xmlns="**default namespace**">
<context:componet-scan base-package="org.example.controller">
</context:componet-scan>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView">
</property>
<property name="prefix">"/WEB-INF/jsp/"
</property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
How to determine what should replace "default namespace"?
You need
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
...
</beans>
You may need to replace thr 3.0 version in schemaLocation with whatever version you're using.