src:
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> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> </struts>
src\chap09:
CheckAction.java
package chap09;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.config.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
@Result("/result.jsp")
public class CheckAction implements ServletRequestAware{
private String name;
private String age;
private String sex;
private HttpServletRequest request;
public String checkPersonalInfo() {
String new_name;
if( "male".equals( sex ) == true ){
new_name = "Mr." + name;
}else{
new_name = "Ms." + name;
}
// 新しい(Mr./Ms.がついた)名前をAttributeで追加
request.setAttribute( "new_name", new_name );
// HttpSessionの作成
HttpSession session = request.getSession( true );
session.setAttribute( "prev_name", name );
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
}
WebContent:
index.jsp
<%@ page language="java" contentType="text/html; charset=windows-31j" pageEncoding="windows-31j"%> <%@ 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=windows-31j"> <title></title> </head> <body> <% String prev_name; if( session != null ){ prev_name = (String)session.getAttribute( "prev_name" ); if( prev_name != null ){ %> <font color="red"> 以前、あなたはアクセスしたことがあります<br> </font> お久しぶりです <%= prev_name %> さん<br> <% } } %> <br> 名前、性別、年齢を入力してください。<br> <br> <s:form action="check" method="post"> <s:textfield key="name" label="氏名" /> <br> <s:radio list="#{'male':'男性','female':'女性'}" name="sex" label="性別"/><br> <s:textfield key="age" label="年齢"/> <br> <s:submit value="submit" method="checkPersonalInfo" /> </s:form> </body> </html>
result.jsp
<%@ page language="java" contentType="text/html; charset=windows-31j" pageEncoding="windows-31j"%> <!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=windows-31j"> <title></title> </head> <body> あなたは、 <br> 氏名: <%= request.getParameter( "name" ) %> <br> <% String sex_string; if( "male".equals( request.getParameter( "sex" ) ) ){ sex_string = "男性"; }else{ sex_string = "女性"; } int age = Integer.parseInt(request.getParameter( "age" )); String drink; if( age >= 20 ){ drink = "あなたはお酒が飲めますね。"; }else{ drink = "あなたはお酒を飲んではいけません。"; } %> 性別: <%= sex_string %> <br> 年齢: <%= request.getParameter( "age" ) %>歳<br> <br> こんにちは <%= (String)request.getAttribute( "new_name" ) %> さん<br> <br> <%= drink %> <br> <a href="/Chap09/index.jsp">トップ</a>へ戻る。 </body> </html>
WebContent\WEB-INF:
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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Chap09</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> <init-param> <param-name>actionPackages</param-name> <param-value> chap09 </param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
WebContent\WEB-INF\lib:
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.11.2.jar
xwork-2.0.5.jar
代码来自日本的技术图书 :http://www.shuwasystem.co.jp/products/7980html/2197.html