kongzy
2024-07-01 47a751cb301d05276ae5d75145d57b2d090fe4e1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package com.nanometer.smartlab.util;
 
import org.primefaces.context.RequestContext;
import org.primefaces.expression.SearchExpressionFacade;
 
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
 
/**
 * JSF的一些方法封装
 */
public class FacesUtils {
 
 
    public static void info(String message) {
        info("", message);
    }
 
    public static void warn(String message) {
        warn("", message);
    }
 
    public static void error(String message) {
        error("", message);
    }
 
    public static void fatal(String message) {
        fatal("", message);
    }
 
    public static void info(String title, String message) {
        addMessage(FacesMessage.SEVERITY_INFO, title, message);
    }
 
    public static void warn(String title, String message) {
        addMessage(FacesMessage.SEVERITY_WARN, title, message);
    }
 
    public static void error(String title, String message) {
        addMessage(FacesMessage.SEVERITY_ERROR, title, message);
    }
 
    public static void fatal(String title, String message) {
        addMessage(FacesMessage.SEVERITY_FATAL, title, message);
    }
 
    public static void addMessage(FacesMessage.Severity severity, String title, String message) {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(severity, title, message));
    }
 
    /**
     * 页面转到别的URL
     *
     * @param url 新的URL
     * @throws IOException
     */
    public static void redirect(String url) throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().redirect(url);
    }
 
 
    public static <T> T getBean(String beanName) {
        FacesContext context = FacesContext.getCurrentInstance();
        return (T) context.getApplication().evaluateExpressionGet(context, "#{" + beanName + "}", Object.class);
    }
 
    /**
     * 运行js语句
     *
     * @param js js代码语句
     * @throws Exception
     */
    public static void js(String js) {
        RequestContext context = RequestContext.getCurrentInstance();
        context.execute(js);
    }
 
 
    public static HttpServletRequest getHttpServletRequest() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        return request;
    }
 
    public static String getHttpRequestParameter(String name) {
        return getHttpServletRequest().getParameter(name);
    }
 
 
    public static void ok() {
        info("操作成功");
    }
 
    /**
     * set cookie value
     *
     * @param name   key of cookie
     * @param value  value of cookie
     * @param expiry an integer specifying the maximum age of the
     *               cookie in seconds; if negative, means
     *               the cookie is not stored; if zero, deletes
     *               the cookie
     */
    public static void setCookie(String name, String value, int expiry) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        Cookie cookie = null;
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (int i = 0; i < cookies.length; i++) {
                if (cookies[i].getName().equals(name)) {
                    cookie = cookies[i];
                    break;
                }
            }
        }
        if (cookie != null) {
            cookie.setValue(value);
        } else {
            cookie = new Cookie(name, value);
            cookie.setPath(request.getContextPath());
        }
        cookie.setMaxAge(expiry);
        HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
        response.addCookie(cookie);
    }
 
    /**
     * get cookie value
     *
     * @param name key of the cookie
     * @return value of the cookie
     */
    public static String getCookie(String name) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (int i = 0; i < cookies.length; i++) {
                if (cookies[i].getName().equals(name)) {
                    return cookies[i].getValue();
                }
            }
        }
        return null;
    }
 
    /**
     * This method returns any UI component whose id is passed.
     *
     * @param clientId the id of the component
     * @return the UIComponent
     */
    public static UIComponent getUIComponent(String clientId) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        if (facesContext != null) {
            UIViewRoot viewRoot = facesContext.getViewRoot();
            if (viewRoot != null) {
                return viewRoot.findComponent(clientId);
            }
        }
        return null;
    }
 
    /**
     * 根据某个基准组件和相对这个组件的表达式,得到对应的组件
     *
     * @param mySourceComponent 基准组件
     * @param expression        表达式
     * @return 对应的组件
     */
    public static UIComponent getComponentFromRelative(UIComponent mySourceComponent, String expression) {
        FacesContext context = FacesContext.getCurrentInstance();
        return SearchExpressionFacade.resolveComponent(context, mySourceComponent, expression);
    }
 
    /**
     * 根据某个基准组件和相对这个组件的表达式,得到对应的组件
     *
     * @param clientId   基准组件ClientID
     * @param expression 表达式
     * @return 对应的组件
     */
    public static UIComponent getComponentFromRelative(String clientId, String expression) {
        UIComponent mySourceComponent = getUIComponent(clientId);
        if (mySourceComponent == null) return null;
        else return getComponentFromRelative(mySourceComponent, expression);
    }
 
 
    /**
     * 根据某个基准组件和相对这个组件的表达式,得到对应的组件绝对路径ID
     *
     * @param mySourceComponent 基准组件
     * @param expression        表达式
     * @return 对应的组件ID
     */
    public static String getComponentIDFromRelative(UIComponent mySourceComponent, String expression) {
        UIComponent componentToUpdate = getComponentFromRelative(mySourceComponent, expression);
        if (componentToUpdate == null) return null;
        else return componentToUpdate.getClientId();
    }
 
 
    /**
     * 根据某个基准组件和相对这个组件的表达式,得到对应的组件绝对路径ID
     *
     * @param mySourceComponentID 基准组件ClientID
     * @param expression          表达式
     * @return 对应的组件ID
     */
    public static String getComponentIDFromRelative(String mySourceComponentID, String expression) {
        UIComponent componentToUpdate = getComponentFromRelative(mySourceComponentID, expression);
        if (componentToUpdate == null) return null;
        else return componentToUpdate.getClientId();
    }
 
}