虽然现在大多数的项目都采用了 sturts2的架构,但是不可避免的有时候要跟旧项目模块的servlet合并共存。
但是我们会发现 当servlet单独存在时,采用<form name="form1" method="post" action="MessagesAction?action=loginRoom">时可以跳转运行的。
加入到sturts2架构后,会报错:
HTTP Status 404 - There is no Action mapped for namespace / and action nameMessagesAction
因为struts2会拦截一切来自浏览器的请求。struts2把servlet当成action了,因为servlet和action都是没有后缀的.
解决方法目前有四种:
方法1-给servlet加后缀
统一在servlet后面加上.servlet结尾(包括web.xml配置文件中和页面上使用servlet的地方)
比如我以上的例子:
index.jsp
<form name="form1" method="post" action="MessagesAction.servlet?action=loginRoom">
MessagesAction.java 不变
package com.cheney.chat;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Random;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class MessagesAction extends HttpServlet {
/**
* Constructor of the object.
*/
public MessagesAction() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
web.xml
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>MessagesAction</servlet-name>
<servlet-class>com.cheney.chat.MessagesAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MessagesAction</servlet-name>
<url-pattern>/MessagesAction.servlet</url-pattern>
</servlet-mapping> |