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>
</body>
</html>
错误信息:
HTTP Status 500 -
--------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: java.lang.NullPointerException
org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:286)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
java.lang.NullPointerException
wu.struts.LoginAction.execute(LoginAction.java:23)
org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs.
[解决办法]
拿分的
[解决办法]
java.lang.NullPointerException
空指针异常
(LoginAction.java:23)
23行值没得到
struts1木用过
[解决办法]
java.lang.NullPointerException
wu.struts.LoginAction.execute(LoginAction.java:23)
估计是你loginForm.getName()这个值为空吧。
[解决办法]
jsp页面的变量是否跟formbean中的name名一致,其他倒是没看出来错误
[解决办法]
你在LoginForm里面定义的是name,而你在Login.jsp里面写的input属性name是username,这两个属性必须是一样的,后台的LoginForm才能取到值。
应该这么写:
//LoginFormprivate String username="";private String password="";
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html