前提・実現したいこと
Compilation errorの回避
発生している問題・エラーメッセージ
delete.scala.htmlではPersonFormのフィールドにアクセスできないと言っている?
variable name in class PersonForm cannot be accessed as a member of controllers.PersonForm from object delete in package html Access to protected variable name not permitted because enclosing object delete in package html is not a subclass of class PersonForm in package controllers where target is defined
該当のソースコード
delete.scala.html
@(message:String, data:PersonForm, id:Int) @main("Delete Person", "DEL", "copyright 2020."){ <p>@message</p> <p>ID = @id</p> <ul> <li>name: @data.name</li> <!-- この行でコンパイルエラー?--> </ul> @helper.form(action=routes.HomeController.remove(id)){ @helper.CSRF.formField <button>Delete Person</button> } }
HomeContoroller.java
public Result delete(int id){ PersonForm form = null; try{ Connection connection = db.getConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("select * from people where id =" + id); resultSet.next(); form = new PersonForm(id); form.setName(resultSet.getString("name")); form.setMail(resultSet.getString("mail")); form.setTel(resultSet.getString("tel")); }catch (SQLException e){ return redirect(routes.HomeController.index()); } return ok(views.html.delete.render("Delete this record.", form, id)); } public Result remove(int id){ try { Connection connection = db.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement("delete from people where id=?"); preparedStatement.setInt(1, id); preparedStatement.executeUpdate(); }catch (SQLException e){ return redirect(routes.HomeController.index()); } return redirect(routes.HomeController.index()); }
Personform.java
package controllers; import java.util.*; public class PersonForm { protected int id; protected String name; protected String mail; protected String tel; public PersonForm(){ super(); } public PersonForm(int id){ super(); this.id = id; } public int getId(){ return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMail() { return mail; } public void setMail(String mail) { this.mail = mail; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } }
routesファイル
GET /delete/:id controllers.HomeController.delete(id:Int) POST /remove/:id controllers.HomeController.remove(id:Int) GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
試したこと
PersonFormのフィールドの可視性をpublicにしたら表示はされましたが、なんか違うような気がします。。。
補足情報(FW/ツールのバージョンなど)
PlayFramework ver2.7.3
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/14 07:26
2020/08/19 16:28