Java Web应用程序web.xml参数转换为POJO
我有一个电子邮件实用程序类,由我的所有 Intranet Web 应用程序共享,该应用程序向员工发送忘记密码的电子邮件(该类被复制到每个 Web 应用程序中)。我需要使用与调用它的应用程序相匹配的适当主题行、回复联系人、应用程序名称等来标记电子邮件。
我可以将这些作为参数传递,但我的方法是在登录网页的标头中包含一个 initialize.jsp
。
<% request.setAttribute("siteEmail", "Commitment@<domain>.com");
request.setAttribute("siteName", "Commitment Report Database");
request.setAttribute("sitePOCEmail", "smith@<domain>.com");
%>
然后,这些请求参数通过 Struts 类似的 Action 存储到 DTO 中。
public class STKRequestPasswordAction implements ControllerAction {
public STKRequestPasswordAction() {}
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ApplicationConfig ac = new ApplicationConfig();
ac.setAppEmail(request.getAttribute("siteEmail").toString());
ac.setAppName(request.getAttribute("siteName").toString());
ac.setPocEmail(request.getAttribute("sitePOCEmail").toString());
request.setAttribute("ac", ac);
userForm.requestPassword(LoginUser, ac);
userForm.requestPassword 方法
public void requestPassword(STKUser loginUser, ApplicationConfig ac) {
验证输入的请求密码的用户 ID,构建 StringBuffer 消息,然后调用 Email Utility 类。
EmailUtils.sendEMailByBadge(loginUser.getBadge(), strFrom, strReplyTo, strSubject, emailBody.toString(), strSMTP, true);
我想从包含的 jsp 中重构我的初始化参数并将它们放入 web.xml 中,但我不确定应该在哪里读取这些值。我在 POJO
String strFrom = getServletContext().getInitParameter("siteEmail");
中尝试过此操作 但POJO中没有servletContext。我想它可以进入我的 servlet,但我的 servlet 使用如下映射:
actionMap.put(null, new STKLoginSubmitAction());
actionMap.put("chkpass", new STKLoginSubmitAction());
actionMap.put("sqlReset", new STKRequestPasswordAction());
String op = request.getParameter("method");
ControllerAction action = (ControllerAction) actionMap.get(op);
我计划将 ApplicationConfig ac
移动到 servlet 并更改 STKRequestPasswordAction
的方法签名以传递‘交流’。这是最好的方法还是我错过了图片?
I have an email utility class that is shared by all my intranet web apps which emails an employee their forgotten password (well the class is copied into each webapp). I need to brand the email with appropriate Subject Line, ReplyTo Point Of Contact, Application Name, etc that matches the app that is calling it.
I'm able to pass these as parameters but the way I do it is by including an initialize.jsp
in my header on the login webpage.
<% request.setAttribute("siteEmail", "Commitment@<domain>.com");
request.setAttribute("siteName", "Commitment Report Database");
request.setAttribute("sitePOCEmail", "smith@<domain>.com");
%>
These request parameter are then stored into a DTO by a Struts similar Action
public class STKRequestPasswordAction implements ControllerAction {
public STKRequestPasswordAction() {}
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ApplicationConfig ac = new ApplicationConfig();
ac.setAppEmail(request.getAttribute("siteEmail").toString());
ac.setAppName(request.getAttribute("siteName").toString());
ac.setPocEmail(request.getAttribute("sitePOCEmail").toString());
request.setAttribute("ac", ac);
userForm.requestPassword(LoginUser, ac);
The userForm.requestPassword method
public void requestPassword(STKUser loginUser, ApplicationConfig ac) {
validates the inputed userID requesting the password, builds the StringBuffer message, and then calls the Email Utility class.
EmailUtils.sendEMailByBadge(loginUser.getBadge(), strFrom, strReplyTo, strSubject, emailBody.toString(), strSMTP, true);
I'd like to refactor my initializing parameters out of the included jsp and put them into the web.xml, but I am not sure where I should be reading the values. I tried this in the POJO
String strFrom = getServletContext().getInitParameter("siteEmail");
but there is no servletContext in the POJO. I would imagine it could go in my servlet but my servlet uses a mapping as follows:
actionMap.put(null, new STKLoginSubmitAction());
actionMap.put("chkpass", new STKLoginSubmitAction());
actionMap.put("sqlReset", new STKRequestPasswordAction());
String op = request.getParameter("method");
ControllerAction action = (ControllerAction) actionMap.get(op);
I plan to move ApplicationConfig ac
to the servlet and change the method signature for STKRequestPasswordAction
to pass `ac'. Is this the best approach or am I missing the picture?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在你的 web.xml 中:
在你的 Java 代码中:
使用 JNDI,你可以在 web 应用程序中的每个位置读取你的设置,如果需要,也可以将你的配置保留在 web.xml 之外,在 Tomcat 的 context.xml 或 jetty-env.xml 中以 Jetty 为例(尽管格式不同)。
In your web.xml:
In your Java code:
Using JNDI you can read your settings every where in your webapp and also keep your configs out of your web.xml if you need, in context.xml for Tomcat or jetty-env.xml for Jetty for example (though format is different).