Alfred’s Computing Weblog

Alfred Java-cored Computing Weblog

Archive for the ‘Spring’ Category

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException

leave a comment »

ERROT:
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException

STORY:
I was setting up a Spring powered web application for testing.
I edited the configurations files (*.xml) to add in some new configuration for testing / POC.
The following error is shown when I deployed the WAR file in tomcat
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 40 in XML document from ServletContext resource [/WEB-INF/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'bean'. One of '{"http://www.springframework.org/schema/beans":description, "http://www.springframework.org/schema/beans":meta, "http://www.springframework.org/schema/beans":constructor-arg, "http://www.springframework.org/schema/beans":property, "http://www.springframework.org/schema/beans":qualifier, "http://www.springframework.org/schema/beans":lookup-method, "http://www.springframework.org/schema/beans":replaced-method, WC[##other:"http://www.springframework.org/schema/beans"]}' is expected.

SOLUTION:
As highlighted above, there is error in the mentioned XML file.
After I opened the XML file, I found that is some missed-closed bean definition ABOVE the mentioned line (Line 40 in XML).
The error bean definition as below:

<bean id="accountManager" class="alfred.sandbox.AccountManager">

Added in a BLACK SLASH “/” to close the bean definition properly will solve the error:

<bean id="accountManager" class="alfred.sandbox.AccountManager"/>
Advertisements

Written by Alfred

September 15, 2011 at 17:40

Plain text shows Encoded Chars

leave a comment »

Just started to have a chance to try out Spring 3.0, a lot of new features introduced, yet not enough time to read up all features.

I started a simple web apps project to try out Spring 3.0. The web apps is pretty simple, a REST server alike. Client init HTTP calls to the web apps with specific Servlet name and parameter, the web apps will returns the result JSON with Plain text mode. Since I am using Spring MVC, the result is printed with JSTL.

Sample call from client: http://myserver.com/getUser?userId=001

Expected result from server:
{“name”:”Bob”,”age”:”30″,”gender”:”male”}

I get what I expected when I test my web apps with browser.

But the ‘interesting’ comes when I test it with little Java Post program (use HttpClient, init HTTP call and inspect the result).
I did not get the expected result, I get below:
{&#034;name&#034;:&#034;Bob&#034;,&#034;age&#034;:&#034;30&#034;,&#034;gender&#034;:&#034;male&#034;}

All double-quat / quotation being replaced with ", its clearly the contain is being HTML encoded.

Below are my trouble-shooting steps:
Step 1. Check tomcat configuration
– Ensure the tomcat UTF-8 by default. More info
Step 2. Check web apps JSPs
– Ensure all JSP are UTF-8 as default encoding.

But both Step 1 & 2 does not fix my problem yet.
Step 3. Google “jstl without html encoded”
– Finally, I get the answer 🙂
– add in escapeXML properties when printing the result in JSTL
Before: <c:out value="${result}" />
After: <c:out value="${result}" escapeXml=”false”/>
Tutorial – Output with / without Encode

Lesson Learn:
When you want to print a PLAIN TEXT with JSTL, do remember to include escapeXML="false" to ensure that end user will get the actual expected result without HTML/XML encoded.

Written by Alfred

June 28, 2011 at 15:54

Posted in HTML, Java, Spring

Tagged with ,

Spring | NoSuchMethodError: GenericTypeResolver

with 4 comments

ERROR: java.lang.NoSuchMethodError: org.springframework.core.GenericTypeResolver.resolveTypeArguments(Ljava/lang/Class;Ljava/lang/Class;)[Ljava/lang/Class;

STORY:
I am starting a new webapps recently, since its a new webapps, I decided to use the latest Spring version 3.1.0.M2. Everything looks fine when the development is still in standalone environment. After dive into the development, the webapps I’m working on need to ‘talk’ to some other existing components in the system. After I have integrated my webapps with the existing components and I restart my tomcat, I get the error java.lang.NoSuchMethodError: org.springframework.core.GenericTypeResolver.resolveTypeArguments(Ljava/lang/Class;Ljava/lang/Class;)[Ljava/lang/Class;

First of all, for Java developer, when you see ‘NoSuchMethodError’, you will directly check the existence of the mentioned method in your JAR file. But ‘sadly’ the mentioned class file and method is there.

SOLUTION:
Thanks to Google, I got my answer in Spring Forum. The answer is pretty simple, I have more than 1 version of Spring in the classpath. As mentioned earlier, the new webapps is running on latest Spring (v3.1.0.M2) but the existing components I integrates with only running on Spring v2.5. So, pls ensure that there is only ONE SPRING VERSION in your classpath.

Written by Alfred

June 20, 2011 at 18:02

java.lang.IllegalArgumentException: Malformed \uxxxx encoding

with 11 comments

The above error is cause by the baclslash “\” that use in Windows.

I demostrate the error in this simple example.
The example is going to get the File object that being instancial by Spring to do further process.

The java code is looks like this:

public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] {
						"classpath:/alfred/demo/spring/applicationContext.xml"
				});
		
		BeanFactory factory = (BeanFactory) context;
		
		final String myFileBeanName = "myFile";
		File file = (File) factory.getBean(myFileBeanName);
		.....
	}
....
}

The applicationContext.xml for Spring:

...
<bean id="myFile" class="java.io.File">
    <constructor-arg>
        <value>${file.path}</value>
    </constructor-arg>
</bean>
...

In the properties file, the $file.path is defined as file.path=D:\data\test\my-spring-file.txt.

When I trying to run the java code, it returns me

Exception in thread "main" java.lang.IllegalArgumentException: Malformed \uxxxx encoding.
at java.util.Properties.loadConvert(Unknown Source)
at java.util.Properties.load(Unknown Source)
....

So, I change the $file.path to file.path=D:\\data\\test\\my-spring-file.txt, and its work fine to me!

Written by Alfred

November 13, 2008 at 15:20

Posted in Java, Spring

Tagged with , ,

The prefix “tx” for element “tx:advice” is not bound

with one comment

Error:
The prefix “tx” for element “tx:advice” is not bound

Story:
I was updating my code, to add in DB data insertion into 1 of my action class.
hit the above error “The prefix “tx” for element “tx:advice” is not bound”
From the error message, i think its complaining the missing of to define the schema location.
So, I add the schema location, and restart the tomcat…. eh, same error again?!
OK, i did a google and found out that, I not only missed to define the schema location;
I also forget to defind the “tx” header. (something like “tablib” in jsp).
So, I add in the following line “xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” and test again.
Wow, its working now!

Lessons Learned:
The root cause of this error is missing the “xmlns:tx” being defined in applicationContext.xml

Solution:
Final applicationContext.xml header:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
>

Written by Alfred

October 17, 2008 at 17:58

Posted in Java, Spring

Tagged with , ,

Intro to Spring Web Flow 2

leave a comment »

The concept of SWF is simple:
In SWF, a flow consists of a series of steps called “State”. Entering a state results in a view being displayed to user. On that view, user event occurs that are handled by the state. These events can trigger transitions to other states which result in view navigation.

    Spring Web MVC

    • SWF is building on top of Spring Web MVC as it provides fundation for developing web application with Spring using MVC paradigm.

    Spring Web Flow

    • SWF is MVC extension, allows to define Controller using Domain-Specific-Language.
    • Domain-Specific-Language is designed to model user interaction that required several requests into server to complete, or may be invoke from different context.

    Spring JavaScript

    • Javascript abstraction framework consists of public JavaScript API with implementation that builds on Dojo.
    • SWF2 distribution includes ease of integration between Spring JavaScript and Spring Web MVC for processing Ajax requests.

    Sprint Faces

    • contains Spring’s support for Java ServerFaces.
    • use JSF as view technology within Spring MVC and SWF Controller.

    Written by Alfred

    July 15, 2008 at 15:31

    Posted in Spring

    Tagged with