Struts2.0直接放jsp文件的异常

 

web.xml文件


<?xml version="1.0" encoding="GB2312"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

version="2.4"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


<filter>

<filter-name>struts2-cleanup</filter-name>

<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>

</filter>


<filter>

<!--过滤器名字  -->

<filter-name>struts2</filter-name>

<!-- 过滤器支持的struts2类  -->

<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

</filter>


<filter-mapping>

<filter-name>struts2-cleanup</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>


<filter-mapping>

<!--过滤器拦截名字  -->

<filter-name>struts2</filter-name>

<!--过滤器拦截文件路径名字  -->

<url-pattern>*.action</url-pattern>

</filter-mapping>


<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>

beanTag.jsp片段,该片段访问一个javabean,并对其中的属性给予赋值
<s:bean name="com.example.struts.action.Material">
<s:param name="materialName" value="一头猪"/>
<s:param name="mainbid" value="1000"/>
<s:param name="mount" value="36"/>
<p>
<s:property value="materialName" />
</p>
<p>
<s:property value="mainbid" />
</p>
<p>
<s:property value="mount" />
</p>
</s:bean>
如果直接访问该jsp就会发生如下异常信息:
异常信息:The Struts dispatcher cannot be found.  This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag.

原因:如果想要在jsp文件中,采用 struts的tag,那么必须通过web.xml所配置的过滤器访问文件,否则会有异常,即 之前所出现的异常。

 

解决方案:

       方案一:

              采用对应的action来访问

       方案二:

             将web.xml 的过滤器,从 *.action 修改为: /*

             或者将为struts2,添加一个新的过滤类型

             

<filter-mapping>

 


<!--过滤器拦截名字  -->

<filter-name>struts2</filter-name>

<!--过滤器拦截文件路径名字  -->

<url-pattern>*.jsp</url-pattern>

     </filter-mapping>



      方案三:

             修改SayHello.jsp 文件,不使用 struts 的标签。

更改后的web.xml

 

<?xml version="1.0" encoding="GB2312"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

version="2.4"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


<filter>

<filter-name>struts2-cleanup</filter-name>

<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>

</filter>


<filter>

<!--过滤器名字  -->

<filter-name>struts2</filter-name>

<!-- 过滤器支持的struts2类  -->

<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

</filter>


<filter-mapping>

<filter-name>struts2-cleanup</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>


<filter-mapping>

<!--过滤器拦截名字  -->

<filter-name>struts2</filter-name>

<!--过滤器拦截文件路径名字  -->

<url-pattern>*.action</url-pattern>

</filter-mapping>


<filter-mapping>

<!--过滤器拦截名字  -->

<filter-name>struts2</filter-name>

<!--过滤器拦截文件路径名字  -->

<url-pattern>*.jsp</url-pattern>

</filter-mapping>


<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>


<?xml version="1.0" encoding="GB2312"?>


 

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

version="2.4"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


<filter>

<filter-name>struts2-cleanup</filter-name>

<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>

</filter>


<filter>

<!--过滤器名字  -->

<filter-name>struts2</filter-name>

<!-- 过滤器支持的struts2类  -->

<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

</filter>


<filter-mapping>

<filter-name>struts2-cleanup</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>


<filter-mapping>

<!--过滤器拦截名字  -->

<filter-name>struts2</filter-name>

<!--过滤器拦截文件路径名字  -->

<url-pattern>*.action</url-pattern>

</filter-mapping>


<filter-mapping>

<!--过滤器拦截名字  -->

<filter-name>struts2</filter-name>

<!--过滤器拦截文件路径名字  -->

<url-pattern>*.jsp</url-pattern>

</filter-mapping>


<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>

 

 

另外:javabean如果要通过页面自动实例化必须提供一个无参的构造函数,因为要通过这个构造函数对javabean进行初始化。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值