I've made a plain JSP application prototype. All pages work right without any trouble. Now, I've added Spring MVC to begin implementing the dynamic behaviour. Just adding DispatcherServlet and with this 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: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">
<context:component-scan base-package="my.controllers" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
Two errors started to appear:
First of all, when I do a:
<%#page import="my.components.Button"%>
<%#page import="my.components.Label"%>
an error appears:
Only a type can be imported. my.components.Label resolves to a package
My package structure is:
my.components
Button
Label
Textbox
etc...
my.model
my.data
...
Second, in a page without imports, when I use a jsp:include (must use it because the JSP fragment uses parameters), this error appears:
org.apache.jasper.JasperException: java.lang.ClassNotFoundException: org.apache.jsp.WEB_002dINF.views.fragments.header_jsp
It seems strange, because without using Spring MVC all runs perfectly. Any idea?
Related
Using the below Spring configuration I load com.Test2 when DEV profile is used and load com.Test1 when in all other cases:
<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.1.xsd">
<bean id="bean1"
class="com.Test1">
</bean>
<beans profile="DEV">
<bean id="bean1"
class="com.Test2">
</bean>
</beans>
</beans>
Moving the Spring profile configuration to the beginning of the file:
<?xml version="1.0" encoding="UTF-8"?>
<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.1.xsd">
<beans profile="DEV">
<bean id="bean1"
class="com.Test1">
</bean>
</beans>
<bean id="bean1"
class="com.Test2">
</bean>
</beans>
the IntelliJ IDE reports the error:
Invalid content was found starting with element '{"http://www.springframework.org/schema/beans":bean}'. One of '{"http://www.springframework.org/schema/beans":beans}' is expected.
Why is this error reported ? Why is it required that the Spring profile be set at the beginning of the file ?
The error is reported because, according to the XML schema, in the second case the elements are in the incorrect order:
As you can see, any <bean> declaration must be provided before any nested <beans>.
This restriction is also indicated in the Spring documentation:
It is also possible to avoid that split and nest <beans/> elements within the same file, as the following example shows:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="...">
<!-- other bean definitions -->
<beans profile="development">
<jdbc:embedded-database id="dataSource">
<jdbc:script location="classpath:com/bank/config/sql/schema.sql"/>
<jdbc:script location="classpath:com/bank/config/sql/test-data.sql"/>
</jdbc:embedded-database>
</beans>
<beans profile="production">
<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/datasource"/>
</beans>
</beans>
The spring-bean.xsd has been constrained to allow such elements only as the last ones in the file. This should help provide flexibility without incurring clutter in the XML files.
I use #Size and #valid annotation in my spring mvc project but it did not work and did not display the message which i want to display.
Download the zip file from the link : http://hibernate.org/validator/downloads/
Extraxt the zip file
Copy all jars of folder hibernate-validator-x.x.x/dist/lib/required
( If hibernate-validator-x.x.x.Final.jar file is not present in the folder then download it and also copy this jar. )
Paste jars into /WEB-INF/lib folder in the spring mvc project.
Add requiered code into the spring-servlet.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.parth.hellocontroller"></context:component-scan>
<mvc:annotation-driven/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/studentmessages"></property>
</bean>
Go to properties of your project
Go to build path and add external jar from downloaded file
Right click on your #size and #valid annotation and import those things
done
I am not able to use "mvc" namespace in my spring configuration file, for a simple spring mvc based web-application which I am developing.
Here is the xml snippet:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.example.controller" annotation-config="true"/>
<context:annotation-config/>
<mvc:annotation-driven/> <-- For this eclipse shows red "*" meaning error
<bean id="viewResolver1" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"> <value>/WEB-INF/</value> </property>
<property name="suffix"> <value>.jsp</value> </property>
</bean>
</beans>
It is not recognising the mvc:annotation-driven tag, but able to recognise other tags from different namespace, for example context:annotation-config.
All this was working fine (I am using Eclipse IDE), only a day before it is giving me this error. I have not changed anything in the environment.
Can anyone please help in how to solve this issue?
I am using Spring 3.1 to create a bean in an web application like below wherein the server contains -DCONFIG_MODE=dev. However, it seems spring is only resolving the filename to configuration.dev without appending the remaining .xml. Could you please point what could be wrong in this.
<?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:ws="http://jax-ws.dev.java.net/spring/core"
xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://jax-ws.dev.java.net/spring/core http://jax-ws.dev.java.net/spring/core.xsd
http://jax-ws.dev.java.net/spring/servlet http://jax-ws.dev.java.net/spring/servlet.xsd">
<bean id="xmlConfig" class="org.quwic.itms.mq.XmlConfiguration" init-method="init">
<constructor-arg type="java.net.URL" value="classpath:configuration.#{systemProperties.CONFIG_MODE}.xml"/>
<constructor-arg type="org.apache.commons.configuration.reloading.ReloadingStrategy" ref="reloadingStrategy"/>
</bean>
<!-- The managed reloading strategy for the configuration bean -->
<bean id="reloadingStrategy" class="org.apache.commons.configuration.reloading.FileChangedReloadingStrategy">
<property name="refreshDelay" value="300000"/>
</bean>
</beans>
Thanks,
Fixed it. I wrongly specified the system property as "-DCONFIG_MODE=local -Dprogram.name=JBossTools: JBoss 5.0 Runtime" rather than -DCONFIG_MODE=local "-Dprogram.name=JBossTools: JBoss 5.0 Runtime"
My controllers are currently mapped to be something like http://example.com/fix.go and, of course, I think that's idiotic and want something nicer like http://example.com/fix or http://example.com/mmm/fix with no extension. When I try to configure this, however, I can't get it to work. I'm clearly missing a key part of understanding on the entire mapping. I'm using Spring 3.x, tomcat, and annotations for the controllers.
My web.xml is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
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">
<servlet>
<servlet-name>BooBoo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>BooBoo</servlet-name>
<url-pattern>*.*</url-pattern> <!-- was *.go when it worked -->
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
BooBoo-servlet.xml is:
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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">
<context:component-scan base-package="com.foofoo.booboo"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
And one of my controllers is configured as such:
#Controller
public class BangBangController extends BaseController {
// Used to be fix.go when it worked
#RequestMapping( value="fix", method=RequestMethod.GET )
public ModelAndView choose(
HttpSession session,
#RequestParam( value="fixId", required=false, defaultValue="-1" ) Integer fixId,
#RequestParam( value="forkId", required=false, defaultValue="-1" ) Integer forkId
)
throws Exception { ... }
}
I've tried changing the mapping in web.xml to /mmm/* expecting URLs like http://example.com/mmm/fix to work, but that too has not worked. I get "missing resource" errors when I enter what I think are the correct URLs in the browser.
What am I goofing up here? What critical piece of understanding do I lack? I've tried making the no extension thing work on another project at work, and couldn't get it to go there either. I'm obviously missing something.
The problem is your dispatcher servlet is matching anything with a "." in it.
Change it to
<url-pattern>/*</url-pattern>
However, that's also bad as you won't be able to serve static content.
What you really should do is this:
<url-pattern>/webapp/*</url-pattern>
Where "webapp" is some prefix. All of your URLs will need to be prefixed with it, but that allows you to still serve static content.
The answer was that I flubbed up the spring-servlet.xml file by not specifying that I was using the Spring MVC stuff. I appreciate Chris' help in diagnosing the issue. I needed confirmation that some of the things I tried (but failed because of this mistake) were correct.
I added
xmlns:mvc="http://www.springframework.org/schema/mvc"
and
<mvc:annotation-driven />
to that file, and boom, it started working. What leaves me scratching my head is, "How did it work without that?" Cuz it worked for *.go patterns.
Here's the resulting file in full:
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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">
<context:component-scan base-package="com.barbar.foofoo"/>
<!-- Enables the Spring MVC #Controller programming model -->
<mvc:annotation-driven />
<!--
Declare View Resolver: when view 'view_name' is called (from the Controller),
the file '/jsp/view_name.jsp' will be used.
-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>