Struts 2変換ウィジェット(Struts 2.3.4)


1.jspログイン画面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'Login2_1.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    : <br/>
	<form action="userAction.action">
		User : <input type="text" name="user"/><br>
		<input type="submit" value=" "/><br/>
		
	
	
	</form>
  </body>
</html>

2.Strtus構成Struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<package name="struts2" extends="struts-default">
		<action name="login" class="Struts2.WildCat.com.LoginAction" method="myExecute">
			<result name="success">/result.jsp</result>
		</action>
		<action name="userAction" class="Struts2.WildCat.com.UserAction">
			<result name="success">/result2_2.jsp</result>
		</action>
		<action name="userAction3" class="Struts2.WildCat.com.UserAction3">
			<result name="success">/result3_1.jsp</result>
		</action>
		<action name="registerAction" class="Struts2.WildCat.com.RegisterAction">
			<result name="success" >/Register_Result.jsp</result>
		</action>
	
	</package>

</struts>

3.Userクラス
package com.WildCat.bean;

public class User {

	private String username;
	private String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

4.タイプ変換プロファイルUserAction-conversion.properties
user=com.WildCat.converter.UserConverter
5.タイプ変換器
package com.WildCat.converter;

import java.util.Map;
import java.util.StringTokenizer;

import com.WildCat.bean.User;
import ognl.DefaultTypeConverter;

public class UserConverter extends DefaultTypeConverter {

	@Override
	public Object convertValue(Map context, Object value, Class toType) {
		
		if (User.class==toType)// 
		{
			String[] str=(String[])value;
			String firstValue=str[0];
			StringTokenizer st=new StringTokenizer(firstValue,":");
			String username=st.nextToken();
			String password=st.nextToken();
			User user=new User();
			user.setUsername(username);
			user.setPassword(password);
			return user;
			
		}
		else if (String.class==toType)// 
		{
			User user=(User)value;
			String username=user.getUsername();
			String password=user.getPassword();
			String userInfo="UserName: "+username+" PassWord: "+password;
			return userInfo;
			
		}
		
		return null;
	}
	
	
}

6.Action
package Struts2.WildCat.com;

import com.WildCat.bean.User;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	@Override
	public String execute() throws Exception {
		System.out.println("UserName: "+user.getUsername());
		System.out.println("PassWord: "+user.getPassword());
		return SUCCESS;
	}
}

7.結果画面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'result2_2.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body> 
   
	<s:property value="user"/>



  </body>
</html>