1

I have just started working on a struts2 project. I have seen the power of actions in struts. i just want to Know few things

1.When a client asks for a page through the search engine does the server direct the request through an action which maps the jsp? 2. If the ans to the above question is no how do we set-up all the bean properties in the action class required for rendering the page? 3.If the ans to the above question is no how to maintain data confidentiality as all interceptors are built around action

SonOfTheEARTh
  • 1,858
  • 5
  • 21
  • 23

2 Answers2

5

If your JSP pages are publicly accessible and a user goes to them directly (e.g., from a search engine or bookmark), then no, your action would not be invoked.

Your JSPs should be placed under the WEB-INF directory (e.g., /WEB-INF/jsp) so that users cannot get to them directly. In Struts2 (any many other MVC frameworks), JSPs are only the templates for your view layer and should not be accessed directly.

There are several comments in reply to one of the answers in Problem with moving JSPs under WEB-INF directory that reinforce this:

I'm not sure about struts, but with Spring, it is accepted practice to put JSPs in WEB-INF and then your view code accesses the protected JSP. This also prevents direct HTTP access to your JSPs so you get better access controls. -- jkf

Same goes for Struts as well. It is considered a good practice to put JSPs in WEB-INF folder. Anyways, I have got my answer. -- craftsman

Community
  • 1
  • 1
Steven Benitez
  • 10,936
  • 3
  • 39
  • 50
  • but then if jsps should be kept in web-inf why do books show outside web-inf.(i have seen struts2 in action keep it out and also in my college teachers while teaching kept it out). also then how do we access them (is it like http://ip:port/proj-name/WEB_INF/xyz.jsp or http://ip:port/proj-name/xyz.jsp) – SonOfTheEARTh Nov 24 '10 at 15:32
  • It is probably for keeping examples simple. It should not be done in practice, though. – Steven Benitez Nov 24 '10 at 15:36
  • 1
    You don't access JSPs directly. You access the action. The action will dispatch to the JSP automatically based on the appropriate result (e.g., `return SUCCESS` or `return INPUT`, etc.) – Steven Benitez Nov 24 '10 at 15:38
  • so does that mean that if i place my jsps in the web-inf folder, then will the search engine be able to locate content in my jsps and will the search engine will use my actions to call the jsps or they will not be found at all – SonOfTheEARTh Nov 25 '10 at 06:40
  • 1
    Yes, search engines would index your actions (which would be displaying the contents of the JSP). Visitors would then be visiting the actions. – Steven Benitez Nov 25 '10 at 15:08
  • It is VERY trivial to access jsp's under web-inf if you create /web-inf/content all put all your jsp's there, then add struts2-conventions-plugin. ie: if you have a jsp /WEB-INF/content/hello.jsp you can access it as http://localhost:8080/hello (well I assumed the application root), and now you enter the world of annotation configuration =) (well there is nothing stopping you from using struts.xml... or struts.xml and annotations and technically there was nothing stopping you from annotating your code in the first place but you would have still needed struts.xml, being accurate is hard!) – Quaternion Jan 23 '11 at 20:46
3

The way Struts works is that it has a dispatcher servlet that reads the path of incoming requests and decides which action to send them to, then the action executes and forwards to a jsp. So whether the action gets called depends on what the url is that the client is clicking on, if it is a url that is mapped to an action in struts then it will call the action, otherwise not.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276