日期:2014-05-20  浏览次数:20663 次

struts初学报错
LoginAction.java:

package wu.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class LoginAction extends Action {

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception {

LoginForm loginForm=(LoginForm)form;


if(loginForm.getName().equals("itcast")){

return mapping.findForward("loginSuccess");

}
else{

return mapping.findForward("loginFailure");
}


}

}

LoginForm.java:

package wu.struts;

import org.apache.struts.action.ActionForm;

public class LoginForm extends ActionForm {


private static final long serialVersionUID = 1L;

private String name=null;

private String password=null;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}

struts-config.xml:
<struts-config>
<form-beans>
<form-bean name="loginForm" type="wu.struts.LoginForm"/>
</form-beans>
<action-mappings>
<action path="/login" type="wu.struts.LoginAction" name="loginForm">
<forward name="loginSuccess" path="/LoginSuccess.jsp"></forward>
<forward name="loginFailure" path="/LoginFailure.jsp"></forward>
</action>
</action-mappings>

</struts-config>


web.xml:
<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/struts-config.xml</param-value>
  </init-param>
  <load-on-startup>0</load-on-startup>
  </servlet>
 
  <servlet-mapping>
  <servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
  </servlet-mapping>
</servlet>


Login.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<html>
  <body>
   
<form action="<%=request.getContextPath() %>/login.do" method= "post">
username:<input type="text" name="username"><br>
password:<input type="password" name="password"><br/>
<input type="submit" value="login">

</form>
  </body>
</html>

LoginSuccess.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<html>
  <body>
  LoginSuccess <br>
  </body>
</html>

LoginFailure.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<html>
  <body>
  LoginFailure <br>