【问题标题】:How to use servlets in struts2struts2中如何使用servlet
【发布时间】:2012-02-10 08:11:35
【问题描述】:

我有一个 struts2 网络应用程序,我想在我的项目中使用一个 servlet,但是 struts2 的过滤器不允许调用 servlet 我已经研究了this 解决方案,但不幸的是它没有解决,问题仍然存在。

有什么想法吗?

servlet 代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.print("morteza");
            OutputStream o = response.getOutputStream();
            InputStream is = new FileInputStream(new File("d:/Desert.jpg"));
            byte[] buf = new byte[32 * 1024]; 
            int nRead = 0;
            while( (nRead=is.read(buf)) != -1 ) {
                o.write(buf, 0, nRead);
            }
            o.flush();
            o.close();
            return; 
    }

struts.xml:

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="false" />
    <constant name="struts.custom.i18n.resources" value="ApplicationResources" />
    <constant name="struts.action.excludePattern" value="Ser"/>
</struts>

web.xml:

      <servlet>
    <description></description>
    <display-name>Ser</display-name>
    <servlet-name>Ser</servlet-name>
    <servlet-class>Ser</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Ser</servlet-name>
    <url-pattern>/Ser</url-pattern>
  </servlet-mapping>

网页:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
fap
<img src="Ser" height="200" width="400"/>


</body>
</html>

【问题讨论】:

  • 什么不起作用?精心制作的。如果您不想拦截所有内容,为什么要将 Struts 过滤器映射到所有内容?
  • 我的项目在 struts 框架中,我根据该答案排除了该 servlet
  • 是的,我明白了。当你尝试这个时发生了什么。有什么例外吗?你的servlet的映射是什么?你在 struts.xml 文件中输入了什么?为什么不使用 Struts 动作而不是这个 servlet?一个 Struts 动作可以完成 servlet 所做的一切。
  • 如何通过 struts2 显示图像?
  • 使用流结果。见struts.apache.org/2.x/docs/stream-result.html。或者执行与您在 servlet 中执行的操作相同的操作:将图像数据写入响应输出流,并从操作中返回 ActionSupport.NONE 或 null,如下所述:struts.apache.org/2.x/docs/result-configuration.html

标签: java struts2 servlet-filters


【解决方案1】:

你可以使用 struts2 Stream Result 来显示图像

 public class ImageAction extends ActionSupport{

        public InputStream getImage() throws FileNotFoundException{
            return new FileInputStream("f://test.jpg");
        }

    }


    <action name="getImage" class="com.project.ImageAction">
                <result name="success" type="stream">
                    <param name="contentType">image/jpeg</param>
                    <param name="inputName">Image</param>
                    <param name="contentDisposition">attachment;filename="image"            </param>
                    <param name="bufferSize">1024</param>
                </result>
    </action>

如果你的项目 url 像 http://localhost:8080/project

<img src="http://localhost:8080/project/getImage" height="200" width="400"/> 

<img src="getImage" height="200" width="400"/>

【讨论】:

    猜你喜欢
    • 2011-07-14
    • 1970-01-01
    • 2015-12-25
    • 2014-09-29
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多