こちらのサイトを参考にScopedModelDrivenを使用したアプリを作成しています。
http://codezine.jp/article/detail/4060
しかし画面遷移するともともとのオブジェクトが破棄されてしまうのか変数の値が消えてしまいます。
ソースコードを記述します。
##ProModel.java
package part2; public class ProModel { // 入力フィールドに対応したメンバ変数 private String name; private String email; private int age; private String career; public String getName(){ return name; } public void setName(String name){ this.name = name; } public String getEmail(){ return email; } public void setEmail(String email){ this.email = email; } public int getAge(){ return age; } public void setAge(int age){ this.age = age; } public String getCareer(){ return career; } public void setCareer(String career){ this.career = career; } }
#Research.java
package part2; import org.apache.struts2.convention.annotation.InterceptorRef; import org.apache.struts2.convention.annotation.InterceptorRefs; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.interceptor.ScopedModelDriven; @InterceptorRefs({ @InterceptorRef(value="scopedModelDriven",params={"scope","session"}), @InterceptorRef("defaultStack") }) public class Research extends ActionSupport implements ScopedModelDriven<ProModel> { private ProModel model; private String scopeKey; @Override public ProModel getModel() { return model; } @Override public void setModel(ProModel model) { this.model = model; } @Override public String getScopeKey() { System.out.println("getScope"); return this.scopeKey; } @Override public void setScopeKey(String key) { System.out.println("setscope"); this.scopeKey = key; } // デフォルトのActionメソッド public String execute() throws Exception { System.out.println("defo"); return "success"; } // submit時のActionメソッド public String confirm() throws Exception { System.out.println("conf"); return "success"; } }
#Reserch.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!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>アンケート</title></head> <body> <s:form action="Research" > <s:textfield label="名前" name="name" scope="session"/> <s:textfield label="メールアドレス" name="email" scope="session"/> <s:textfield label="年齢" name="age" /> <s:radio label="どの携帯のCMが好き?" name="career" list="#{'1':'ドコモ', '2':'au', '3':'ソフトバンク'}"/> <s:submit value="確認" action="ResearchConfirm" /> <s:debug /> </s:form> </body> </html>
#confirm.jsp
<%@ page language="java" contentType="text/html; charset=Windows-31J" pageEncoding="Windows-31J"%> <%@ taglib prefix="s" uri="/struts-tags" %> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>確認</title> </head> <body> <s:form action="Research"> 名前:<s:property value="name"/><br /> メールアドレス:<s:property value="email"/><br /> 年齢:<s:property value="age"/><br /> キャリア:<s:property value="career"/><br /> <s:submit value="確定" action="ResearchConfirm1" /> <s:debug /> </s:form> </body>
#conform1.jsp
<%@ page language="java" contentType="text/html; charset=Windows-31J" pageEncoding="Windows-31J"%> <%@ taglib prefix="s" uri="/struts-tags" %> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>確認</title> </head> <body> 名前:<s:property value="name"/><br /> メールアドレス:<s:property value="email"/><br /> 年齢:<s:property value="age"/><br /> キャリア:<s:property value="career"/><br /> <s:debug /> </body>
#struts.xml
<?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="Pro" extends="struts-default"> <action name="Hello" class="part1.Hello"> <result name="success">/index.jsp</result> </action> <action name="Research" class="part2.Research"> <result name="success">/part2/research.jsp</result> </action> <action name="ResearchConfirm" method="confirm" class="part2.Research"> <result name="success">/part2/confirm.jsp</result> </action> <action name="Research" class="part2.Research"> <result name="success">/part2/research.jsp</result> </action> <action name="ResearchConfirm" method="confirm" class="part2.Research"> <result name="success">/part2/confirm.jsp</result> <result name="input">/part2/research.jsp</result> </action> <action name="ResearchConfirm1" method="confirm" class="part2.Research"> <result name="success">/part2/conform1.jsp</result> </action> </package> </struts>
書ききれないのでweb.xmlは省略しますがそこに問題がありそうなら記載します。
環境は先ほどのサイトと同じのはずです。
ScopedModelDrivenでセッションに値が保存されているはずですが保存されません。
confirmからconform1に画面遷移する際にコンソールに
情報: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
と赤字で表示されました。
どこを修正したらいいでしょうか。
回答2件
あなたの回答
tips
プレビュー