Я использовал перехватчики для аутентификации в своем приложении. Я добавил его в defaultStackHibernate
. Его рабочий тон &. Я получаю желаемые результаты. Единственная проблема в том, что сейчас я хотите показать имя пользователя в заголовке каждой страницы.Покажите имя пользователя в заголовке каждой страницы в Struts 2
Я попробовал это <s:property value="name"></s:property>
, но работает только для страницы приветствия. Так есть способ, которым я могу отправить некоторую переменную, например, имя пользователя из перехватчика, в каждое действие, которое я вызываю, или непосредственно в jsp?
если у меня есть доступ к другим действиям, я получаю сеанс нулевой спасибо.
Следующая часть моего кода страницы заголовок JSP:
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<b><s:property value="name"></s:property></b> <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="user_profile.html">My Profile</a></li>
<li class="divider"></li>
<li><a href="logOut">Log Out</a></li>
</ul>
</li>
следующий мой
LoginAction
класс:
public class LoginAction extends ActionSupport implements SessionAware, ModelDriven<User>{
private static final long serialVersionUID = -3369875299120377549L;
private User user = new User();
private SessionMap<String, Object> sessionAttributes = null;
private List<User> userList = new ArrayList<User>();
private UserDAO userDAO = new UserDAOImpl();
@Override
public String execute(){
System.out.println("inside execute");
if(user.getName().equals(user.getName()) && user.getName().equals(user.getPassword())){
System.out.println("Name"+user.getName() +" password"+user.getPassword());
sessionAttributes.put("USER", user);
return SUCCESS;
}
return INPUT;
}
@Override
public void setSession(Map<String, Object> sessionAttributes) {
this.sessionAttributes = (SessionMap<String, Object>)sessionAttributes;
}
@Override
public User getModel() {
return user;
}
public String logout(){
if(sessionAttributes!=null){
sessionAttributes.clear();
sessionAttributes.invalidate();
}
return "success";
}
}
AuthenticationInterceptor
класс:
public class AuthenticationInterceptor implements Interceptor {
private static final long serialVersionUID = -5011962009065225959L;
@Override
public void destroy() {
//release resources here
}
@Override
public void init() {
// create resources here
}
@Override
public String intercept(ActionInvocation actionInvocation)
throws Exception {
System.out.println("inside auth interceptor");
Map<String, Object> sessionAttributes = actionInvocation.getInvocationContext().getSession();
User user = (User) sessionAttributes.get("USER");
if(user == null){
return Action.LOGIN;
}else{
Action action = (Action) actionInvocation.getAction();
if(action instanceof UserAware){
((UserAware) action).setUser(user);
}
return actionInvocation.invoke();
}
}
struts.xml
:
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<package name="default" extends="hibernate-default">
<interceptors>
<interceptor name="authentication"
class="com.inwi.interceptors.AuthenticationInterceptor"></interceptor>
<interceptor-stack name="authStack">
<interceptor-ref name="authentication"></interceptor-ref>
<interceptor-ref name="defaultStackHibernate"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="authStack"></default-interceptor-ref>
<global-results>
<result name="loginAction" type="redirect">/home.action</result>
</global-results>
<action name="home">
<interceptor-ref name="defaultStack"></interceptor-ref>
<result>/login.jsp</result>
</action>
<action name="loginAction" class="com.inwi.action.LoginAction">
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success">/dashboard.jsp</result>
<result name="input">/loginError.jsp</result>
</action>
<action name="welcome" class="com.inwi.action.WelcomeAction">
<result name="success">/dashboard.jsp</result>
</action>
<action name="saveOrUpdateTypeSites" method="saveOrUpdate" class="com.inwi.action.TypeSitesAction">
<result name="success" type="redirect">listTypeSites</result>
</action>
<action name="listTypeSites" method="list" class="com.inwi.action.TypeSitesAction">
<result name="success">/typeSiteMain.jsp</result>
</action>
эта работа для меня спасибо –
Student
@Student Это можно сократить до просто ' '. Вы также можете использовать JSP EL, например, '$ {USER.name}' –