0

Is there is any way I can write the Java code in JSF in the same manner as we do in JSP using script-lets?

I want to write following code in JSF:

<% 
EditProperties editProperties = new EditProperties();
Map<String,String> dataMap = new HashMap<String,String>();
dataMap = editProperties.getDocumentProperties();
if (dataMap.size() > 0) {
    Iterator<Map.Entry<String,String>> entries = dataMap.entrySet().iterator();
    while (entries.hasNext()) {
        Map.Entry<String, String> entry = entries.next();  
%>
Durgesh Sahu
  • 247
  • 1
  • 5
  • 19
  • 5
    JSF and other technologies were invented to **avoid** scriptlets in jsp. Believe me, you don't want to add scriptlets. What's your functional requirement? What specific problem you have moving this code on your managed bean? – Luiggi Mendoza Oct 02 '12 at 04:48
  • Thanks Luiggi for replying, actually in my jsf page i have to use the basic input component So in order to iterate over this component i need that jav acode inside my jsf page – Durgesh Sahu Oct 02 '12 at 04:57
  • Any valuable info to help people get involved in your problem must be in your question, not in comments. Edit your question and add everything you consider good so people could understand the situation and help you. – Luiggi Mendoza Oct 02 '12 at 04:59
  • 1
    Based in your comment, you must learn the basics of JSF. Read the info on our [JSF wiki](http://stackoverflow.com/tags/jsf/info), your actual approach is very outdated. – Luiggi Mendoza Oct 02 '12 at 05:01
  • The very goal of JSF is to ensure that your business logic goes in a seperate file rather than the JSP itself.So please move this code to your managed bean. – Chaitanya Gudala Oct 02 '12 at 08:02
  • 1
    Related: http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files – BalusC Oct 02 '12 at 11:29

1 Answers1

2

There is a lot of work involved in not allowing you to do this.

As Luiggi mentioned - take some time learning JSF basics and see what it can give you. Another good source of knowledge is Core JavaServer Faces book by D. Geary and C. Horstmann.

If you still can't achieve what you want using JSF core libraries (or some additional, widely available ones), then any Java code related with front-end should be in your controller classes like JSF managed beans. It will make your code easier to maintain and will allow you to test it.

There is nothing worse than putting scriptlets in your JSP/JSF/whatever view technology.

Piotr Nowicki
  • 17,914
  • 8
  • 63
  • 82