【发布时间】:2013-06-26 13:09:50
【问题描述】:
我有 HttpServlet。它将用户重定向到不同的 jsp 页面,这取决于用户想要执行的操作。
例如http://localhost:8080/collections/index.do 重定向到 index.jsp。
我像这样保存在 picocontainer 中的不同操作
<component-instance key="index">
<com.epam.collections.web.IndexAction/>
</component-instance>
当用户在浏览器中写入之前的 url -
1) 我得到动作名称 - index
String name = getActionName(req);
2) 从 picocontainer 获取操作
Action action = (Action) pico.getComponentInstance(name);
3) 执行动作 - 返回代表 jsp 页面名称的字符串
String view = action.exec(req, resp);
exec方法在哪里
public String exec(HttpServletRequest req, HttpServletResponse resp) {
return "index";
}
4) 转发用户至index.jsp page
getServletContext().getRequestDispatcher(
"/WEB-INFO/pages/" + view + ".jsp").forward(req, resp);
当 picocontainer 中没有操作时,我想将用户转发到页面 notfound.jsp。例如一些blabla.do 应该返回notfound.jsp 页面。但是当我喜欢这样的时候
if (action == null) {
getServletContext().getRequestDispatcher(
"/WEB-INF/jsp/notfound.jsp").forward(req, resp);
return;
}
因为当 xml 文件中不存在操作时,getComponentInstance 返回 null - 我收到错误 500
当我写一些没有.do 的东西时,我也想重定向到这个页面。例如ddd.dd、plain 等。但是我遇到了 404 错误。
【问题讨论】:
标签: java servlets picocontainer