2017-03-19 57 views
0

命令按钮导致此异常:HTTP状态500 - /[email protected],110 action =“#{encryptBean.encrypt(#{encryptBean.path})}”无法解析表达式[#{encryptBean.encrypt(# {} encryptBean.path)}]我如何将参数从xhtml传递到支持bean方法?

<?xml version="1.0" encoding="ISO-8859-1" ?> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:c="http://java.sun.com/jsf/core" 
      xmlns:ui = "http://java.sun.com/jsf/facelets" 
      xmlns:h = "http://java.sun.com/jsf/html"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
    <title>encryption</title> 
    </head> 
    <h:body> 
     <h:form> 
      <h3>Please enter file path:</h3> 

      <label>Path:</label> 
      <h:inputText value="#{encryptBean.path}" /> 
      <h:commandButton value="Encrypt" type="submit" action="#{encryptBean.encrypt(#{encryptBean.path})}"/> 
     </h:form> 
    </h:body> 
    </html> 


package bean; 

import java.io.IOException; 
import java.io.PrintWriter; 

import javax.faces.bean.ManagedBean; 

import encryptor1.EncryptDecrypt; 


@ManagedBean 
public class EncryptBean 
{ 
    private String path="zzz"; 

    public String getPath() { 
     return path; 
    } 

    public void setPath(String path) { 
     this.path = path; 
    } 

    public static void encrypt(String path) throws IOException, Exception 
    { 
     //String path= "C:\\Users\\User\\Desktop\\secret.txt"; 
     String encrypted= EncryptDecrypt.encrypt(EncryptDecrypt.readFileAsString(path),path); 
     try 
     { 
      PrintWriter writer = new PrintWriter(EncryptDecrypt.setFilePath(path,"_encrypted"), "UTF-8"); 
      writer.println(encrypted); 
      writer.close(); 
     } catch (IOException e) {}   
    } 


} 

回答

0

当你读/在管理bean写path领域,你可能只是使操作按钮方法不带参数那样:

<h:commandButton ... action="#{encryptBean.encrypt()}"/> 

并据此在方法的签名中:

public static void encrypt(/* Nothing here */) throws IOException, Exception { 

您应该的方式,使该方法非静态以便能够使用外地path的,或让两者static的。倾向于使用第一种方法。

+0

encrypt()是静态的,因此将无法使用_path_属性。他也应该删除关键字。 –

+0

你是对的,谢谢。编辑。 – Omar

相关问题