Running servlet from faces-config
It will be hard to explain.. So, in a.jsp I have something like this:
<h:form enctype="multipart/form-data" >
<td><input type="text" name="imgName" value="" size="7"/></td>
<td><input type="text" name="imgDesc" value="" size="30"/></td>
<td>
<x:upload target="/upload/#{loginBean.user.login}/#{loginBean.user.filesUploaded}_image.jpg"/>
</td>
<td>
<h:commandButton value="Send" action="submit"/>
</td>
</h:form>
Now, pressing "Send" button will run filter, because (as I assume) that filter runs for every Faces Servlet (so for every jsp?). My web.xml file:
<filter>
<filter-name>Upload Filter</filter-name>
<filter-class>DomainModels.Adds.UploadFilter</filter-class>
<init-param>
<param-name>sizeThreshold</param-name>
<param-value>1024</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Upload Filter</filter-name开发者_JAVA百科>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
How can I force running servlet after filtering? I've read somewhere that I have to write simple <servlet>
and <servlet-mapping>
. Ok, so I've changed "action" attribute to "TestServlet.do", added this:
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>DomainModels.Adds.AddImage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet.do</url-pattern>
</servlet-mapping>
but still my TestServlet doesn't work.. I was trying to add into faces-config.xml this:
<navigation-rule>
<from-view-id>/upload/a.jsp</from-view-id>
<navigation-case>
<from-outcome>submit</from-outcome>
<to-view-id>/TestServlet.do</to-view-id>
</navigation-case>
</navigation-rule>
but it gives no result.. Any ideas?
Replace <h:form>
by <form>
so that you can utilize the action
attribute.
Alternatively, don't use a servlet, but just use a managed bean action the JSF way. You only need to implement decode()
of that homegrown x:upload
component accordingly that it puts the uploaded file which is collected by the filter as a bean property. Or if that's too much work, just reuse an existing JSF file upload component, like Tomahawk's t:inputFileUpload
. How to use it can be found in this article.
精彩评论