wood burning stoves 2.0*
The moose likes JSF and the fly likes <to-view-id> Element Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "<to-view-id> Element" Watch "<to-view-id> Element" New topic
Author

<to-view-id> Element

Sunny Jassal
Greenhorn

Joined: Feb 08, 2011
Posts: 13
Hello,

I am new to JSF and trying to learn. I researched this but I was not able to find any solution. It is possible that I am trying to do something that is not possible.

Here is what I am trying to do:

I am trying a simple test to see how navigation works. I was able to a navigate to JSP page using navigation rule with no problem. I now wanted to see if I can navigate to a servlet so I changed <to-view-id> element to point to a servlet rather than a JSP. First of all can I even do that? I would think that this should be possible as JSP is compiled into a servlet. However all tutorials and examples so reference for <to-view-id> to a JSP or xhtml files.

Below is a snippet of my faces-config.xml and web.xml. When I access the address http://localhost:8080/adminTool/faces/saveServlet, the servlet loads fine. However when I click on a button on http://localhost:8080/adminTool/faces/test.jsp, I get the message:

type Status report

message/saveServlet not found

descriptionThe requested resource (/saveServlet not found) is not available.

Any help in the matter will be greatly appreciated.

Thanks in advance.

faces-config.xml

<navigation-rule>
<from-view-id>test.jsp</from-view-id>
<navigation-case>
<from-outcome>saveServlet</from-outcome>
<to-view-id>saveServlet</to-view-id>
</navigation-case>
</navigation-rule>

web.xml :

<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>saveServlet</servlet-name>
<servlet-class>com.mypackage.saveServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>saveServlet</servlet-name>
<url-pattern>/faces/saveServlet</url-pattern>
</servlet-mapping>
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15959
    
  19

SaveSevlet is not a view ID. It's a servlet. When using the to-view-ID, JSF can only dispatch to JSF views.


Customer surveys are for companies who didn't pay proper attention to begin with.
Sunny Jassal
Greenhorn

Joined: Feb 08, 2011
Posts: 13
First of all thank you for your reply. I appreciate the response.

From your answer, it is what I thought. What I was trying to do was not possible. However you mentioned that view id cannot be used as a servlet only JSF, but is there anyway to post to servlet? I am more used to follow the following convention:

1. Create form in JSP
2. Post the JSP to servlet
3. Let servlet do the business logic
4. Return back to JSP

While learning JSF, I am trying to follow the same convention. It seems like either I am not understanding the concept correctly or JSF is meant so the business logic is directly written into the JSF. Would you be able to explain the concept?
Sunny Jassal
Greenhorn

Joined: Feb 08, 2011
Posts: 13
I have been thinking about this and I think I now understand what I am trying to do. Should I be writing the business logic in the bean instead? For example:

Bean: lookup.java
JSF: myForm.jsp/.xhtml
JSF: output.jsp/x.html

In the myForm.jsp will forward it to a page output.jsp. In output.jsp i will simply call lookup.saveLookup()? If I am correct, is there an example to see this in works? Lastly, is there anyway of posting to the page itself and actually doing the logic within the page? Here is what I am thinking:

In the beginning of the myForm.jsp, I check a parameter. If the parameter is true then call lookup.saveLookup() prior to showing the form. myForm.jsp view id will point to itelf. I am trying to avoid posting to multiple files. Am I on the right track?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15959
    
  19

No. DO NOT put logic in the page (View). JSF is all about MVC, and it won't be MVC if you blatantly break the MVC architectural rules.

The only "logic" that should appear in a view is view-related stuff such as (simple) EL that supports View attributes such as rendered=, enabled= and so forth.

Complex View logic and very definitely business logic should be behind the Backing Bean.

In simple webapps, you can put the business logic in the Backing Bean. In more complex webapps, it's often preferable to put the business logic in beans of their own (such as Session EJBs). The primary purpose of the backing bean is to serve as the Model for the JSF View, so separating the functionality out often makes it easier to maintain the app and to use reusable business components without tying them too closely to the JSF framework.
Sunny Jassal
Greenhorn

Joined: Feb 08, 2011
Posts: 13
Hmmm it is starting to make sense now, I think

Below is what I think by example. Please let me know if my logic here is correct. If it is would you be able to answer one question below?

Going to create 4 files:

lookup.xhtml - holds the form for a bean DOLookup
DOLookup. java - bean
savelookup.xhtml - lookup.xhtml will call this page
Database.java - a seperate java class that holds all database information and executes the queries

- Database will have a function called saveLookup(DOLookup l) - it will be responisble for connecting to the database and passing the information to the stored procedure.
- DOLookup will have a function called save(). It will intatiate Database and then call saveLookup(this)

faces-config.xml will look like

<navigation-rule>
<from-view-id>lookup.xhtml</from-view-id>
<navigation-case>
<from-outcome>saveLookup</from-outcome>
<to-view-id>saveLookup.xhtml</to-view-id>
</navigation-case>
</navigation-rule>

Does the above look correct? If it is then how can I call DOLookup.save()? I tried doing "#{lookupValue.save()}" in saveLookup.xhtml on its own but it did not work.

Another thought was to get rid of saveLookup.xhtml and moving "#{lookupValue.save()}" into faces-config.xml file. Would that work?

Sorry if these questions are not very smart
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: <to-view-id> Element
 
Similar Threads
Getting Cannot find FacesContext and sometime showing html codes in the page
navigation problems
using XHTML & JSP in the same application
Cannot find FacesContext and showing the html code in the jsp
Problem running a simple JSF application