• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

<to-view-id> Element

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SaveSevlet is not a view ID. It's a servlet. When using the to-view-ID, JSF can only dispatch to JSF views.
 
Sunny Jassal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Nothing up my sleeve ... and ... presto! A tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic