开发者

Struts2 page redirect/forward problem

开发者 https://www.devze.com 2023-03-11 18:05 出处:网络
Can anyone tell me how to forward or redirect page with Struts? I\'m checking if the session is null or not in every jsp and want to redirect if null. I can do this with simple jsp:forward or response

Can anyone tell me how to forward or redirect page with Struts? I'm checking if the session is null or not in every jsp and want to redirect if null. I can do this with simple jsp:forward or response.sendRedirect() but i want to redirect to a struts2 action I declared in my struts.xml. I tried putting action names in above mentioned tag and method but it didn't work.

if(session.getAttribute("user")!=null)
    response.sendRedirect("loginAction");

Is there anyway to do this.I can't call to a .jsp directly because I have add a security const开发者_运维技巧raint to *.jsp url pattern in my web.xml. So i want to do this forward or redirect using actions. Any help?


It's best to do this checking in a Filter or an Interceptor. This answer to a similar question may be of help.


First, perhaps you'll prefer to check for authorized use (login) in an interceptor (eg)

Second, to do a redirect in Struts2 to a struts2 action, you just use the appropiate result. It's bad practice to access the lower level objects (servlet related) the if it's not necessary.

Third, about I can't call to a .jsp directly: you cannot redirect to an jsp url (because the jsp cannot be accesed from the browser, in your architecture), but you can dispath to it, that is, show that login.jsp in response to your action. This is straightforward, only objetionable if you want the user to see some '... login.do' url in his browser when he is asking for authentication.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号