Carregando um arquivo

6 respostas
Jedi_FeniX

Estou querendo jogar nos campos de form os valores de um arquivo “.properties”. Como eu posso fazer isso com Struts2?

6 Respostas

TangZero

Você quer editar as propriedades do arquivo properties?

Jedi_FeniX

Isso eu quero editar o arquivo quando o from for submetido…
Mas antes eu queria mostrar os valores que já existem no arquivo e colocar nos campos do form.
O problema que eu estou tendo é na hora de abrir o arquivo .properties, eu recebo uma exception, java.io.FileNotFoundException, que quer dizer que o arquivo não foi achado.
Sabe em qual path o tomcat roda as actions do Struts2?

TangZero

Jedi_FeniX:
Isso eu quero editar o arquivo quando o from for submetido…
Mas antes eu queria mostrar os valores que já existem no arquivo e colocar nos campos do form.
O problema que eu estou tendo é na hora de abrir o arquivo .properties, eu recebo uma exception, java.io.FileNotFoundException, que quer dizer que o arquivo não foi achado.
Sabe em qual path o tomcat roda as actions do Struts2?

Poste o código da Action.

Jedi_FeniX

A action é esta:

package control;

import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;

import java.util.Properties;

import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.ActionSupport;


public class ControlConfiguration extends ActionSupport implements Preparable{
        private int thread;
        private String host;
        private String port;
        private String databaseName;
        private String username;
        private String password;
        private String pathRequest;
        private String pathFileControl;
        private String destinationPath;
        private String destinationHost;
        private Properties properties;
        private String pathProperties = "../config/config.properties";

        // Nome dos campos das propriedades
        private static String THREAD = "THREAD";
        private static String DATABASE_HOST = "DATABASE_HOST";
        private static String DATABASE_PORT = "DATABASE_PORT";
        private static String DATABASE_NAME = "DATABASE_NAME";
        private static String DATABASE_USERNAME = "DATABASE_USERNAME";
        private static String DATABASE_PASSWORD = "DATABASE_PASSWORD";
        private static String PATH_SAVE_REQUEST = "PATH_TXT";
        private static String FILE_CONTROL_IMPORTER = "FILE_CONTROL_IMPORTER";
        private static String DESTINATION_PATH = "DESTINATION_PATH";
        private static String DESTINATION_HOST = "DESTINATION_HOST";

        private Properties getProperties(){
                return this.properties;
        }
      
        //Metodo para carregar as propriedades na memória
        private void setProperties(){
           try{    
              FileInputStream input = new FileInputStream( this.pathProperties );
              this.properties = new Properties();
              this.properties.load(input);
              input.close();
              this.setThread(Integer.parseInt(this.getProperties().getProperty(ControlConfiguration.THREAD)));
              this.setHost(this.getProperties().getProperty(ControlConfiguration.DATABASE_HOST));
              this.setPort(this.getProperties().getProperty(ControlConfiguration.DATABASE_PORT));
              this.setUsername(this.getProperties().getProperty(ControlConfiguration.DATABASE_USERNAME));
              this.setPassword(this.getProperties().getProperty(ControlConfiguration.DATABASE_PASSWORD));
              this.setPathRequest(this.getProperties().getProperty(ControlConfiguration.PATH_SAVE_REQUEST));              
       this.setPathFileControl(this.getProperties().getProperty(ControlConfiguration.FILE_CONTROL_IMPORTER));                  
              this.setDestinationPath(this.getProperties().getProperty(ControlConfiguration.DESTINATION_PATH));
              this.setDestinationHost(this.getProperties().getProperty(ControlConfiguration.DESTINATION_HOST));
                } catch(FileNotFoundException e){
                        e.printStackTrace();
                  } catch(IOException e1){
                        e1.printStackTrace();
                    }
        }

        public void prepare(){
                this.setProperties();
        }
       
        public String execute(){
                this.getProperties().setProperty(ControlConfiguration.THREAD, String.valueOf(this.getThread()));
                this.getProperties().setProperty(ControlConfiguration.DATABASE_HOST, this.getHost());
                this.getProperties().setProperty(ControlConfiguration.DATABASE_PORT, this.getPort()); 
                this.getProperties().setProperty(ControlConfiguration.DATABASE_NAME, this.getDatabaseName());
                this.getProperties().setProperty(ControlConfiguration.DATABASE_USERNAME, this.getUsername());
                this.getProperties().setProperty(ControlConfiguration.DATABASE_PASSWORD, this.getPassword());
                this.getProperties().setProperty(ControlConfiguration.PATH_SAVE_REQUEST, this.getPathRequest());
                this.getProperties().setProperty(ControlConfiguration.FILE_CONTROL_IMPORTER, this.getPathFileControl());
                this.getProperties().setProperty(ControlConfiguration.DESTINATION_PATH, this.getDestinationPath());
                this.getProperties().setProperty(ControlConfiguration.DESTINATION_HOST, this.getDestinationHost());
                try{
                        FileOutputStream output = new FileOutputStream( this.pathProperties );
                        this.getProperties().store(output, "******* Properties ******");
                        output.close();
                } catch(IOException e){
                        e.printStackTrace();
                  }
                return SUCCESS;
        }
TangZero

Como está estrutura (árvore) da sua aplicação?

Jedi_FeniX

Resolvi o problema.
A solução ficou assim, primeiro eu criei um objeto para carregar as propriedades na memória, depois a action chama este objeto. E ai a action joga os valores no campo.
Obrigado pela ajuda de todos!

Criado 21 de maio de 2008
Ultima resposta 27 de mai. de 2008
Respostas 6
Participantes 2