在使用struts进行web开发时,点击链接<a href="${pageContext.request.contextPath}/category.do?method=addCategory">Add Category</a>进行提交时出现如下错误:
: Servlet.service() for servlet action threw exception java.lang.NoSuchMethodException: Action[/category] does not contain specified method (check logs) at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:264) at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187) at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431) ......
web.xml配置片段如下:
...... <!-- Standard Action Servlet Configuration --> <servlet> <servlet-name>action</servlet-name> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/config/struts-config.xml</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <!-- Standard Action Servlet Mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> ......
struts-config.xml配置如下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> <struts-config> <global-forwards></global-forwards> <form-beans> <form-bean name="categoryForm" type="com.king.springsecurity.form.CategoryForm" /> </form-beans> <action-mappings> <action path="/category" type="com.king.springsecurity.action.CategoryAction" parameter="method" name="categoryForm"> <forward name="saveSuccess" path="/pages/categoryList.jsp" /> <forward name="listCategory" path="/pages/categoryList.jsp" /> </action> </action-mappings> </struts-config>
CategoryAction.java代码如下:
package com.king.springsecurity.action; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; import com.king.springsecurity.form.CategoryForm; import com.king.springsecurity.model.Category; import com.king.springsecurity.service.ICategoryService; import com.king.springsecurity.service.impl.CategoryServiceImpl; public class CategoryAction extends DispatchAction { private ICategoryService categoryService = new CategoryServiceImpl(); public void setCategoryService(ICategoryService categoryService) { this.categoryService = categoryService; } public ActionForward addCategory(HttpServletRequest request, HttpServletResponse response,ActionMapping mapping, ActionForm form) throws Exception { CategoryForm categoryForm = (CategoryForm) form; Category category = new Category(); category.setName(categoryForm.getName()); category.setDescn(categoryForm.getDescn()); categoryService.addCategory(category); List<Category> categoryList = new ArrayList<Category>(); categoryList = categoryService.listCategory(); request.getSession().setAttribute("categoryList", categoryList); return mapping.findForward("saveSuccess"); } }
经检查,web.xml和struts-config.xml中的配置是正确的,CategoryAction.java看似没什么错误,但问题就出现在CategoryAction的addCategory()的方法中,具体原因是addCategory()参数的数序不对,正确的顺序应该是:
public ActionForward addCategory(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {......}