在action中 不能有execute方法,否则其他的方法则不能执行.
如下:
package com.yanek.mvc.demo;
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;
public class TestAction extends DispatchAction
{
public ActionForward execute(ActionMapping mapping, ActionForm actionform,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserForm form=(UserForm)actionform;
String username=form.getUsername();
String password=form.getPassword();
System.out.println("parametor..."+mapping.getParameter());
System.out.println("username..."+username);
System.out.println("password..."+password);
if (username.equals("admin") && password.equals("admin"))
{
mapping.findForward("success");
}
else
{
mapping.findForward("success");
}
System.out.println("action execue index...");
return mapping.findForward("success");
}
public ActionForward add(ActionMapping mapping, ActionForm actionform,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserForm form=(UserForm)actionform;
String username=form.getUsername();
String password=form.getPassword();
System.out.println("username..."+username);
System.out.println("password..."+password);
System.out.println("action add ...");
return mapping.findForward("add");
}
public ActionForward update(ActionMapping mapping, ActionForm actionform,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserForm form=(UserForm)actionform;
String username=form.getUsername();
String password=form.getPassword();
System.out.println("username..."+username);
System.out.println("password..."+password);
System.out.println("action update ...");
return mapping.findForward("update");
}
public ActionForward delete(ActionMapping mapping, ActionForm actionform,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserForm form=(UserForm)actionform;
String username=form.getUsername();
String password=form.getPassword();
System.out.println("username..."+username);
System.out.println("password..."+password);
System.out.println("action delete ...");
return mapping.findForward("delete");
}
public ActionForward list(ActionMapping mapping, ActionForm actionform,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserForm form=(UserForm)actionform;
String username=form.getUsername();
String password=form.getPassword();
System.out.println("username..."+username);
System.out.println("password..."+password);
System.out.println("action list ...");
return mapping.findForward("list");
}
}
由于 action里有execute方法,所以其他的方法都不能执行,执行其他方法时都会执行execute方法.
把execute改名或去掉即可.
配置:
<action path="/test" type="com.yanek.mvc.demo.TestAction" name="userForm" scope="request" parameter="action">
<forward name="success" path="/test/index.jsp"/>
<forward name="add" path="/test/add.jsp"/>
<forward name="update" path="/test/update.jsp"/>
<forward name="delete" path="/test/delete.jsp"/>
<forward name="list" path="/test/list.jsp"/>
</action>
访问:
http://127.0.0.1:8080/struts1Demo/test.do?action=list
http://127.0.0.1:8080/struts1Demo/test.do?action=add
http://127.0.0.1:8080/struts1Demo/test.do?action=update
http://127.0.0.1:8080/struts1Demo/test.do?action=delete
执行相应的方法
如果有execute方法,则执行如上方法都会进入execute方法.