Gráfico JFree Chart + Struts2

3 respostas
D

Pessoal,

É meu primeiro post aqui no fórum e espero muito q vocês possam me ajudar.

Estou fazendo um sistema em Struts2, e preciso gerar um gráfico com algumas informações desse sistema.

Alguém poderia postar um pequeno exemplo, que funcione para gerar gráfico em Struts2.

Se puder ser um exemplo completo com xml, action, jsp e java.

Obrigada,

Abraços.

3 Respostas

EvaristoJava

Tambem preciso gerar um grafico com Jfreechat usando o Struts2.
Mas nao aparece o grafico na pagina.
Você conseguiu um exemplo que funcione ??

D

Evaristo,

Conseguimos gerar o gráfico sim, utilizamos um exemplo de tutorial da internet de struts1, porém quando colocava em nosso projeto, o gráfico ficava com um X na frente, não mostrando a imagem.

Esse problema ocorria devido a uma configuração no xml, que permite a visualização de alguns arquivos. No meu xml estava * (todos), mas por algum motivo não deixava ler o png, então tirei o * e coloquei um por um, png, jsp, jpg, etc.

Faz muito tempo que mexi com isso, mas se quiser posso tentar achar algo lá no pc de casa.

Att,

Daniela

EvaristoJava

Boa noite !!

Você fala do Welcome file List

<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
		
	</welcome-file-list>
ou na configuração do filtro do Struts
<filter>
   <filter-name>Struts2</filter-name>
   <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<filter>
 
  <filter-mapping>
  <filter-name>Struts2</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>

Pois no Welcome File List eu coloquei as tags

<welcome-file>default.png</welcome-file>
		<welcome-file>default.jpg</welcome-file>

E continuou nao aparecendo o Grafico Segue abaixo os codigos do Struts.xml e da action

<?xml version="1.0" encoding="UTF-8" ?> 
   <!DOCTYPE struts PUBLIC 
	"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
	"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

	<package name="default" extends="struts-default, jfreechart-default">

		<action name="grafico" class="evaristo.teste.chart.action.MyChartAction">

			<result name="ok" type="chart">
			
				<param name="value">chart</param>
				<param name="type">png</param>
				<param name="width">640</param>
				<param name="height">480</param>

			</result>

		</action>


	</package>

</struts>

Action

package evaristo.teste.chart.action;

/*import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;*/
import org.jfree.chart.JFreeChart;

import com.opensymphony.xwork2.ActionSupport;

import teste.chart.Chart;

public class MyChartAction extends ActionSupport {	
	private JFreeChart chart;
	
//	@Action(value="chart", results={
//		@Result(name="ok",location="/teste.jsp")
//	})
	public String execute(){
		
		System.out.println("cheguei ");
		Chart c = new Chart();
		/*chart = c.criaChart();*/
		setChart(c.criaChart());
		
		return "ok";
	}

	private JFreeChart getChart() {
		return chart;
	}

	private void setChart(JFreeChart chart) {
		this.chart = chart;
	}
	

	
}

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table border="2" align="center">
<tr>
<td >
"Ola MundO"
<img src="chart.jsp">
</td>

</tr>
</table>

</body>
</html>

chart.jsp

<% response.sendRedirect("grafico");%>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>StrutsChartXML</display-name>
	
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
		<welcome-file>default.png</welcome-file>
		<welcome-file>default.jpg</welcome-file>
		
	</welcome-file-list>

  <filter>
   <filter-name>Struts2</filter-name>
   <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class><!--
   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
 --></filter>
 
  <filter-mapping>
  <filter-name>Struts2</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>

Obrigado pela ajuda !!

Criado 10 de dezembro de 2009
Ultima resposta 18 de dez. de 2010
Respostas 3
Participantes 2