3

I am having two folders in my WEB-INF.

Folder1 contains 3 jsp like page1.jsp , page2.jsp , page3.jsp .

Folder2 contains the page4.jsp.

In dispatcher-Serlet.xml I have configured like ,

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/folder1/" />
    <property name="suffix" value=".jsp" />
</bean>

In Controller ,

@RequestMapping(value="page4.html",method=RequestMethod.GET)
public  String getTransfer(){

    List<String> list = new LinkedList<String>();
    Hashtable<String , Object> map = MyHashtableClasss.getDetails;

    if(!map.isEmpty()){
        Set<String> keys = map.keySet();
        for(String key: keys){
                list.add(map.get(key).getUserName());
        }
    }
    model.addAttribute(list);

    return "page4";
}

How can I get page4.jsp in to the view . Because I didn't have that page4.jsp in Folder1.

In general , How can show the different JSP's in differenf folders in Spring.Is that possible ? If not , what is the alternative.

Hope our stack user will help me.

xwoker
  • 3,105
  • 1
  • 30
  • 42
Human Being
  • 8,269
  • 28
  • 93
  • 136

2 Answers2

6

1 Change

<property name="prefix" value="/WEB-INF/folder1/" />

to

<property name="prefix" value="/WEB-INF/" />

and return view with folder path

return "folder1/page4";

2 Other way is to configure multiple view resolver beans and set the priority for each. Check following links for solution and limitations.

Multiple View resolvers in spring mvc

Spring MVC with multiple view resolvers

EDIT

The other way is to build your own view resolver by extending InternalResourceViewResolver. In extended view resolver you can add logic to scan sub folders and find the jsp file with the returned view name.

Community
  • 1
  • 1
Sachin Gorade
  • 1,427
  • 12
  • 32
0

It's your choice what folder to specify for the ViewResolver.

If you specify /folder1, the JSPs will be resolved under folder1. If you don't, you'll need to specify /folder1 yourself when you want it.

You can use a function, or a constant, to do that for you if you really need to or it helps factoring..

public class Views {
    public static String legacy (String name) {return "folder1/"+name;}
    public static String modern (String name) {return "folder2/"+name;}
}

If you define Controllers in a separate application-context, you can hook them up with a separately-configured ViewResolver (with a different path).

But all this seems pretty obvious to me, I'm not really sure why you're asking such a question. Can Spring magically know that for one view, you want a different folder? I hope not, I'd prefer it to just work reliably in the folder that I specify.

Thomas W
  • 13,940
  • 4
  • 58
  • 76