Java 重定向到自定义未找到的jsp页面
我有一个HttpServlet。它将用户重定向到不同的jsp页面,具体取决于用户想要执行的操作。 例如Java 重定向到自定义未找到的jsp页面,java,servlets,picocontainer,Java,Servlets,Picocontainer,我有一个HttpServlet。它将用户重定向到不同的jsp页面,具体取决于用户想要执行的操作。 例如http://localhost:8080/collections/index.do重定向到index.jsp。 我在picocontainer中保留的不同动作如下 <component-instance key="index"> <com.epam.collections.web.IndexAction/> </component-instance>
http://localhost:8080/collections/index.do
重定向到index.jsp。
我在picocontainer中保留的不同动作如下
<component-instance key="index">
<com.epam.collections.web.IndexAction/>
</component-instance>
if (action == null) {
getServletContext().getRequestDispatcher(
"/WEB-INF/jsp/notfound.jsp").forward(req, resp);
return;
}
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页面
getServletContext().getRequestDispatcher(
"/WEB-INFO/pages/" + view + ".jsp").forward(req, resp);
当picocontainer中没有操作时,我想将用户转发到页面notfound.jsp
。例如,一些blabla.do
应该返回notfound.jsp
页面。但是当我喜欢这个的时候
<component-instance key="index">
<com.epam.collections.web.IndexAction/>
</component-instance>
if (action == null) {
getServletContext().getRequestDispatcher(
"/WEB-INF/jsp/notfound.jsp").forward(req, resp);
return;
}
因为当xml文件中不存在操作时,getComponentInstance
返回null
,所以出现错误500
另外,当我在没有的情况下写东西时,我想重定向到这个页面。例如ddd.dd
,plain
等等,但我有404错误。1)要处理404错误,需要在web.xml中设置404处理程序
2) 您犯了500个错误,因为您的jsp页面可能有问题,您需要仔细检查jsp代码
3) 使用pico的方式是一种反模式,请查看pico文档