struts怪异问题
web.xml
<?xml version= "1.0 " encoding= "UTF-8 "?>
<web-app>
<display-name> Struts Hibernate Spring </display-name>
<welcome-file-list>
<welcome-file> index.jsp </welcome-file>
</welcome-file-list>
<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>
</servlet>
<servlet-mapping>
<servlet-name> action </servlet-name>
<url-pattern> *.do </url-pattern>
</servlet-mapping>
<taglib>
<taglib-uri> /WEB-INF/struts-bean.tld </taglib-uri>
<taglib-location> /WEB-INF/struts-bean.tld </taglib-location>
</taglib>
<taglib>
<taglib-uri> /WEB-INF/struts-html.tld </taglib-uri>
<taglib-location> /WEB-INF/struts-html.tld </taglib-location>
</taglib>
<taglib>
<taglib-uri> /WEB-INF/struts-logic.tld </taglib-uri>
<taglib-location> /WEB-INF/struts-logic.tld </taglib-location>
</taglib>
<taglib>
<taglib-uri> /WEB-INF/struts-template.tld </taglib-uri>
<taglib-location> /WEB-INF/struts-template.tld </taglib-location>
</taglib>
</web-app>
struts-config.xml
<?xml version= "1.0 " encoding= "UTF-8 "?>
<struts-config>
<action-mappings>
<action path= "/HelloWorld "
type= "hello.HelloAction "
name= "HelloForm "
scope= "request "
input= "index.jsp ">
<forward name= "SayHello " path= "index.jsp "/>
</action>
</action-mappings>
<form-beans>
<form-bean name= "HelloForm " type= "hello.HelloForm "/>
</form-beans>
<message-resources parameter= "hello.app "/>
</struts-config>
HelloAction.java
package hello;
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 HelloAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
String hf = (String)((HelloForm)form).getUserName();
request.setAttribute( "username ", hf);
return mapping.findForward( "SayHello ");
}
}
HelloForm.java
package hello;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class HelloForm extends ActionForm {
/**
*
*/
private static final long serialVersionUID = 1L;
private String userName = null;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.userName = null;
}
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors msg = new ActionErrors();
if (userName == null) {
msg.add( "username ", new ActionMessage( "index.jsp.no.username.error "));
}
return msg;
}
}
index.jsp
<%@ page language= "java " contentType= "text/html; charset=windows-31j "
pageEncoding= "windows-31j "%>
<%@ taglib uri= "/WEB-INF/struts-html.tld " prefix= "html " %>
<%@ taglib uri= "/WEB-INF/struts-bean.tld " prefix= "bean " %>
<%@ taglib uri= "/WEB-INF/struts-logic.tld " prefix= "logic " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN " "http://www.w3.org/TR/html4/loose.dtd ">
<html:html locale= "true ">
<head>
<title> <bean:message key= "index.jsp.title "/> </title>
<html:base/>
</head>
<body>
<p>
<bean:message key= "index.jsp.header "/>
</p>
<br>
<p>
<html:errors/>
</p>
<p>
<logic:present name= "username " scope= "request ">
<bean:message key= "index.jsp.say.hello "/>
<bean:write name= "username "/>
</logic:present>
</p>
<p>
<html:form action= "/Welcome.do " focus= "userName ">
<bean:message key= "index.jsp.page.username "/>
<html:text property= "userName "> </html:text> <br>
<html:submit> </html:submit>
<html:reset> </html:reset>
</html:form>
</p>
</body>
</html:html>
控制台报错
致命的: サーブレット jsp のServlet.service()が例外を投げました
java.lang.NullPointerException
at org.apache.struts.taglib.TagUtils.retrieveMessageResources(TagUtils.java:1219)
at org.apache.struts.taglib.TagUtils.message(TagUtils.java:1082)
at org.apache.struts.taglib.bean.MessageTag.doStartTag(MessageTag.java:226)
at org.apache.jsp.index_jsp._jspx_meth_bean_message_0(org.apache.jsp.index_jsp:180)
at org.apache.jsp.index_jsp._jspx_meth_html_html_0(org.apache.jsp.index_jsp:123)
at org.apache.jsp.index_jsp._jspService(org.apache.jsp.index_jsp:93)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)
[解决办法]
空指针错误。哪个对象内容为null了。
[解决办法]
<message-resources parameter= "hello.app "/>
查看你的资源文件是否配置正确,然后看看页面用到的key在资源文件里是否有定义~