Struts2配置时常见的问题_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Struts2配置时常见的问题

Struts2配置时常见的问题

 2014/4/21 0:19:00  在下个路口  程序员俱乐部  我要评论(0)
  • 摘要:struts2的配置问题:配置步骤:一:添加jar包,常用的有八个jar文件注意jar包的冲突问题,比如用到xml解析的时候xml-apis.jar和xerces-2-6-2就会和JDK1.6冲突,冲突时会报如下错误:Caughtexceptionwhileloadingfilestruts-default.xml-[unknownlocation]atcom.opensymphony.xwork2.config.providers.XmlConfigurationProvider
  • 标签:配置 问题 struts
struts2的配置问题:
  
      配置步骤:
          一:添加jar包,常用的有八个jar文件

  注意jar包的冲突问题,比如用到xml解析的时候xml-apis.jar和xerces-2-6-2就会和JDK1.6冲突,冲突时会报如下错误
class="java" name="code">Caught exception while loading file struts-default.xml - [unknown location]
	at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadConfigurationFiles(XmlConfigurationProvider.java:839)
	at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadDocuments(XmlConfigurationProvider.java:131)
	at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.init(XmlConfigurationProvider.java:100)
	at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reload(DefaultConfiguration.java:130)
	at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:52)
	at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:395)
	at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:452)
	at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:203)
	at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:281)
	at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:262)
	at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:107)
	at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4775)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5452)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1282)
	at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1446)
	at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:322)
	at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
	at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
	at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1374)
	at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1530)
	at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
	at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1519)
	at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ClassCastException: org.apache.xerces.parsers.XML11Configuration cannot be cast to org.apache.xerces.xni.parser.XMLParserConfiguration
	at org.apache.xerces.parsers.DOMParser.<init>(Unknown Source)
	at org.apache.xerces.parsers.DOMParser.<init>(Unknown Source)
	at org.apache.xerces.jaxp.DocumentBuilderImpl.<init>(Unknown Source)
	at org.apache.xerces.jaxp.DocumentBuilderFactoryImpl.newDocumentBuilder(Unknown Source)
	at com.sun.org.apache.xalan.internal.xsltc.trax.SAX2DOM.<init>(SAX2DOM.java:69)
	at com.sun.org.apache.xalan.internal.xsltc.runtime.output.TransletOutputHandlerFactory.getSerializationHandler(TransletOutputHandlerFactory.java:187)
	at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getOutputHandler(TransformerImpl.java:392)
	at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerHandlerImpl.setResult(TransformerHandlerImpl.java:137)
	at com.opensymphony.xwork2.util.DomHelper$DOMBuilder.setup(DomHelper.java:213)
	at com.opensymphony.xwork2.util.DomHelper$DOMBuilder.<init>(DomHelper.java:198)
	at com.opensymphony.xwork2.util.DomHelper$DOMBuilder.<init>(DomHelper.java:189)
	at com.opensymphony.xwork2.util.DomHelper$DOMBuilder.<init>(DomHelper.java:175)
	at com.opensymphony.xwork2.util.DomHelper.parse(DomHelper.java:115)
	at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadConfigurationFiles(XmlConfigurationProvider.java:830)
	... 23 more


   二:配置struts.xml
      配置的标签格式就不多说了,就提提需要注意的问题。
       (1)添加xml头部信息,别粗心。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

缺少的话会是404.
      (2)<action name="LoginAction" class="com.huaxin.struts.LoginAction">  name的值必须和jsp页面中想要请求的Action一致。不一致时报如下错误
java.lang.NullPointerException
	org.apache.struts2.impl.StrutsActionProxy.getErrorMessage(StrutsActionProxy.java:69)
	com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
	org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
	org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
	com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
	org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:501)
	org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:432)

因为是通过jsp中form的提交路径找到struts配置中的该Action类。
     (3)struts2中struts2配置文件文件名必须为struts.xml,在struts1中为struts—config.xml,而且该文件必须存放在项目中的src目录下,千万别放到WEB-INF下了。
   三:Action类的实现
       (1)写该类的时候在继承ActionSupport的时候记得重写execute()方法就行了。
       (2)该类中所用到的参数名需和jsp页面表格中提交的参数名一致。比如类中有这两个属性
String username;
String password;
那么jsp中参数名必须一致。而get(),set()方法最好用快捷键实现,如果采用以上属性名,则get()方法必须为getUsername() 注意大小写的错误。
     上面这些问题都是刚刚配置的时候遇到的问题,总之一切得细心细心再细心,一个字母的差错,说不定会浪费你一两个小时的时间去发现这个错误。
  • 大小: 22.3 KB
  • 查看图片附件
发表评论
用户名: 匿名