views:

47

answers:

1

I'm new to both JSF and Spring Framework and I'm trying to figure out how to make them work together. My current problem is that application outputs my JSF files without interpreting them. Here are some snippets of my code which I believe might be relevant:

dispatcher-servlet.xml

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="login.htm">loginController</prop>
        </props>
    </property>
</bean>

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/pages/"
      p:suffix=".xhtml" />

<bean name="loginController" class="controller.LoginController" />

loginController

public class LoginController extends MultiActionController {
 public ModelAndView login(HttpServletRequest request,
   HttpServletResponse response) throws Exception {
    System.out.println("LOGIN");
    return new ModelAndView("login");
}

WEB-INF/pages/login.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"&gt;
<h:head>
    <title>#{message.log}</title>
</h:head>
<h:body>
    <h:form>
        <h:outputLabel value="#{message.username}" for="userName">
            <h:inputText id="userName" value="#{User.name}" />
        </h:outputLabel>            
        <h:commandButton value="#{message.loggin}" action="#{User.login}" />
    </h:form>
</h:body>
</html>

Any ideas where the problem might be? Does this code make any sense at all? I'm well aware of fact, that probably completely sucks and I'll be glad to here WHY it sucks and how to make it better. Thanks :)

EDIT: I'm adding piece of code which seems to be the root of the problem and which I (of course) didn't include in the original question:

web.xml

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>

The url-pattern of Faces Servlet had to be changed to *.xhtml in order to work properly.

+2  A: 

If the JSF tags are not been parsed, then it simply means that the request URL did not match the url-pattern of the FacesServlet as definied in web.xml. It is namely responsible for all that JSF happenings. You need to verify if the request URL matches the url-pattern of the FacesServlet. If it is for example *.jsf, then you need to invoke it by http://example.com/context/page.jsf and thus not by http://example.com/context/page.xhtml.

I am however not sure how Spring fits in the picture since I don't use it, but to get JSF to work you really need to pass the request through the FacesServlet first, not through some Spring controller. Spring should do its job thereafter.

BalusC
I'm trying to change web.xml as you said and I'm getting these errors:WARNING: ApplicationDispatcher[/PodleRose2] PWC1231: Servlet.service() for servlet jsp threw exceptionjava.lang.RuntimeException: Cannot find FacesContextWARNING: StandardWrapperValve[dispatcher]: PWC1406: Servlet.service() for servlet dispatcher threw exceptionjava.lang.RuntimeException: Cannot find FacesContextWhat does that mean?
Corvus
Geez... now it works but I'm even more frustrated than I was before because I'm 98% sure I tried the same setting at least 3x before and it didn't work :-OIs that Netbeans or is this standard behaviour in field of Java web applications? :-D
Corvus
The former exception will indeed be thrown when you don't pass the request through the `FacesServlet`. It's the one responsible for creating the `FacesContext`. The JSF components in the XHTML page are complaining about that since they require the `FacesContext` to function properly. Glad that you got it working anyway :)
BalusC
Anyway.. thanks for help, it seems to do what I expect it to do (for a change). Now I'm can hopefully get to my original application-scope-bean problem from yesterday :)
Corvus