Как использовать сервлеты в стойках2 - PullRequest
0 голосов
/ 14 января 2012

У меня есть веб-приложение struts2, и я хочу использовать сервлет в своем проекте, но фильтр struts2 не позволяет вызывать сервлеты. Я рассмотрел это решение, но, к сожалению, оно не сработалои проблема остается.

Есть идеи?

код сервлета:

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>

1 Ответ

0 голосов
/ 19 января 2012

Вы можете использовать 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"/>
...