Is there a way to pass a variable into a method param:
<h:commandButton value="Add to Order"
actionListener="#{orderBasket.addItems(currentItem.id)}"/>
This always seems to pass 0 into the method for some reason.
That's only possible when you use action instead of actionListener
<h:commandButton value="Add to Order"
action="#{orderBasket.addItems(currentItem.id)}"/>
and you're running a Servlet 3.0 / EL 2.2 capable container (Tomcat 7, Glassfish 3, JBoss 6, etc) and your web.xml is declared conform Servlet 3.0 spec with the following root declaration
<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_3_0.xsd"
version="3.0">
If the latter two are not true for your case (e.g. you're using Servlet 2.5), then you need to replace the EL implementation by another one which supports that, such as JBoss EL. For detail, see this answer.
Related
I creating a web project using maven, java 7, String framework, hibernate, JPA and restful service in Eclipse. I changed the jre library in Java Build Path and also change the compiler.
but now i got error
Cannot change version of project facet Dynamic Web Module to 3.0.
thanks in advance !!!
The solution given by #enkor may work for you (Question is the same):
Cannot change version of project facet Dynamic Web Module to 3.0?
you need to change the version into web.xml you need change some configuration you can find it below:
<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_3_0.xsd"
version="3.0">
<display-name>Servlet 3.0 Web Application</display-name>
</web-app>
then Update Project.
I have a webapp with Maven which uses Tomcat plugin for the server. The app gets compiled to .war which, when extracted, seems to contain all classes (incl. servlets) in WEB-INF/classes folder.
When the url http://localhost:8080/com.galya.crm gets hit, index.html (which is a SPA app) gets loaded normally redirecting to http://localhost:8080/com.galya.crm/#!/login/msg/notlogged
I have 4 servlets annotated in a similar manner:
#WebServlet("/restapi/login")
public class LoginController extends HttpServlet {
The problem comes when the SPA app tries to authenticate using the login servlet (shown above). I expect it to be here: http://localhost:8080/com.galya.crm/restapi/login , but I get 404 error.
Below I attached the Tomcat plugin folder that is automatically created. Work directory is empty and I'm not sure if it's OK.
Initially the webapp/WEB-INF/web.xml was auto generated and contained the following:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>
I also tried to change it to accept WebServlet annotations, but didn't work also:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<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_3_0.xsd"
version="3.0">
</web-app>
P.S. The app was working on another server some time ago, so the problem should be in the server configurations, not in the code itself.
Surely the cause is that the deployment descriptor must be declared to be of version 3.0. Your web.xml is still 2.3.
Remove the DOCTYPE declaration and leave the root node just as you already did:
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
Tested myself, and it worked.
I've been struggling for 2-3 weeks now with transforming my project (single module) into a multi module project where the modules are representing the layers of a multilayer architecture (i.e. persistence, business, domain...)
When the project was still a single module, everything worked together. I had my ManagedBean classes managed by JSF and enabled #Autowired for Spring with a workaround, so I could inject my Spring-managed classes into the jsf-managed beans. Now, this won't seem to work anymore, so I'm trying to clean my code up and orientate myself at some examples I've seen (one is the Maven archetype for jsf2 + spring 4 integration).
It seems, there are two ways suggested to do the Dependency Injection:
Do it per #ManagedProperty (But I don't want to do this all per JSF)
Do it per #Autowired and let Spring manage the Backing/Managed Beans (That's what I try to do)
Though many sources (even the Maven archetype, which is working) suggest to just annotate my Backing Bean class with Spring annotations (#Component, #Scope("session")), I always get the error
Caused by: javax.el.PropertyNotFoundException: Target Unreachable, identifier 'administration' resolved to null
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/WEB-INF/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>
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd" >
<context:component-scan
base-package="my.base.projectpackage" />
<context:annotation-config />
<context:spring-configured />
<!-- import the other context files for Spring -->
<import resource="classpath*:META-INF/*-springContext.xml"/>
faces-config.xml
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2" metadata-complete="false">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
I've already tried to:
Enable Autowiring into a JSF managed bean (per [...].autowire(this) -> not working, NPE for the Service class)
Try it with #ManagedProperty on JSF annotated Service classes (not working, NPE for the Service class)
Try it with pure Spring classes (like mentioned above, not working identifier resolved to null for the managed bean)
I can compile and build the modules and the whole project without errors, and I've also not forgotten to put intermodular dependencies (like business module is depending on persistence module) into the pom.xml. Every module has packaging "jar" (except ofc the webapp module, which is war). Did I forget to mention something else?
I run the project on Tomcat 8 (though this should not be the source of the error).
Maybe someone of you has got a hint, idea or solution for me.
Thanks in advance.
Edit
I got my project to work again and split it into multiple modules. But the only way to do this was to really copy + paste (argh! really..) my classes into the Maven archetype project and add some minor, not important fixes (like cleaning out my POM files). When it worked there, I was curious, copied the project files back into my old project and tried it there again. Surprisingly, it still didn't work, even though there was no difference anymore which could have caused it not to work. So, the mystery is still unsolved, but after all, I could continue with my work.
Thank you for your help (or for trying to help me, since this was a lost cause) #Tiny.
Update: Somehow, after another round of adjustments and redeployment, localhost:8080/ping-1.0/ping started working. Configuration files are still as below. I wish I knew what I fixed without knowing it, but it is solved now.
I've been wrestling with this for a couple days, tried all sorts of solutions I've seen here and elsewhere, and nothing has worked. I have a Spring MVC controller deployed in Tomcat, but can't access it.
Tools:
Spring 3.2.0
Tomcat 7
Java 1.6
Spring controller:
#Controller
public class PingController {
#RequestMapping("/ping")
public String ping (Model model) throws Exception {
System.out.println("ping ping ping");
String s = (new Date()).toString();
model.addAttribute("message", s);
return "ping";
}
}
web.xml:
<?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>ping</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/ping-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ping</servlet-name>
<url-pattern>/ping</url-pattern>
</servlet-mapping>
</web-app>
ping-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:mvc="http://www.springframework.org/schema/mvc"
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="myclass.ping"/>
<mvc:annotation-driven />
</beans>
The WAR file is called ping-1.0.war. Deployment seems to go fine. I see a directory called ping-1.0 in $CATALINA_BASE/webapps and this in catalina.log:
INFO: Mapped "{[/ping],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String myclass.ping.PingController.ping(org.springframework.ui.Model) throws java.lang.Exception
Tomcat is running on port 8080. I can access localhost:8080/manager for instance. But localhost:8080/ping returns a 404 message from Tomcat. I see nothing in the logs other than a record of a GET request. No errors at all. I've tried a lot of variations of request mapping, URL filter, etc. and just can't get this to work.
You don't have the context root on your URL and you actually need to have /ping/ping because both the dispatcher servlet and your ping controller are mapped to /ping. Try this:
http://localhost:8080/ping-1.0/ping/ping
#RequestMapping("/ping")
Means /ping relative to the URL the dispatcher servlet listens on.
<url-pattern>/ping</url-pattern>
Here comes the problem. This makes your dispatcher servlet listen to one URL and one URL only. And that is assumingly localhost:8080/ping-1.0/ping. But your controller method is relative to that, so it would be localhost:8080/ping-1.0/ping/ping, and the disptacher servlet does not react on that URL. You have to use a pattern:
<url-pattern>/ping/*</url-pattern>
Now the dispatcher servlet can listen on all URLs starting with localhost:8080/ping-1.0/ping.
One final note: Depending on your configuration it could be that you have to omit ping-1.0.
I think you've configured Spring incorrectly.
I'd expect the servlet to be Spring's dispatcher servlet, not your ping controller. That's what figures out where to route the request. You don't have a front controller servlet.
I could be thinking Spring 2.x and earlier. I'll admit that I'd be incorrect if Spring 3.x changed the need for the dispatcher. But that's the way my applications are set up.
I work on a web app using Spring annotations to inject my Controllers and Services. In the app, I have users working on projects but I need to make sure one project is only edited by one user at a time so I have an attribute in the table "Project" in the database to save if the project is open and by whom.
I need to add an HTTPSessionListener to be able to "close" the project when the user editing it disconnects. It means that at the "sessionDestroyed" event, I want to call my DAO service to update the project in the database.
The only problem is that this service is injected by Spring and I cannot get it...
I tried to use #Autowired in my HTTPSessionListener but it didn't work, I tried to do like in this solution (Spring – How to do dependency injection in your session listener ) but I get a nullPointerException for the WebApplicationContext...
My 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:jsp="http://java.sun.com/xml/ns/javaee/jsp"
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">
<!-- jsp config => automatic inclusions in all jsp files -->
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<include-prelude>taglibs.jsp</include-prelude>
<include-prelude>setLanguage.jsp</include-prelude>
</jsp-property-group>
</jsp-config>
<display-name>MEANS</display-name>
<!--Spring dispatcher servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern><!-- detect all urls ending with ".do" -->
</servlet-mapping>
<!-- The welcome files -->
<welcome-file-list>
<welcome-file>connection.jsp</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>30</session-timeout><!-- the session is automatically disconnected after 30 min of inactivity -->
</session-config>
<listener>
<listener-class>myPackages.listener.MySessionListener</listener-class>
</listener>
And the dispatcher-servlet.xml :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Spring MVC Support for annotations (JSR-303) -->
<mvc:annotation-driven/>
<!-- Spring View resolver => find the jsps -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:suffix=".jsp" />
<!-- Spring info : what packages to scan for detecting annotations -->
<context:component-scan base-package="myPackages"/>
So I need your help... Does anybody have an idea of how I could inject a Spring Service into my HTTPSessionListener?
Thanks in advance for your help!
One of the options (perhaps the best one from the design point of view) is to create a bean of scope session (don't forget to declare RequestContextListener) and put logic associated with the session lifecylce into it. Spring will automatically call its destruction method (annotated with #PreDestroy or configured as destroy-method) when session is to be destroyed.
Your approach with injection into session listener causes problems because you only have DispatcherServlet, not ContextLoaderListener, and WebApplicationContextUtils cannot obtain an application context associated with DispatcherServlet. If you choose to follow that apporach you should create a root application context and extract some of your beans into it, then you will be able to access it from session listener via WebApplicationContextUtils.