Chinaunix首页 | 论坛 | 博客
  • 博客访问: 336898
  • 博文数量: 96
  • 博客积分: 2041
  • 博客等级: 大尉
  • 技术积分: 1080
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-20 14:08
文章分类

全部博文(96)

文章存档

2015年(2)

2013年(1)

2012年(93)

分类: 系统运维

2012-01-20 14:25:25

全局类型转换:所有的Action里只要包含了对应的类的对象,就会执行全局的类型转换。在src目录下新建xwork-convertion.properties,该文件的内容是待转换的类=转换器的名字,即com.shengsiyuan.bean.User=com.shengsyuan.converter.UserConverter2(全局使用较少,一般尽量使用局部转换)

 

input.jsp

<%@ page contentType = "text/html" pageEncoding="gbk"%>

<html>

<body>

使用分号;隔开用户名和密码

    <form action = "userAction.action">

   

    user info 1:<input type = "text" name = "user"><br>

    <input type = "submit" value = "submit">

 

form>

body>

html>

 

struts2.xml部分:

<action name = "userAction" class = "com.shengsiyuan.struts2.UserAction">

       <result name = "SUCCESS">/output.jspresult>

       action>

 

(Action为:UserAction.java,包含了对应类的对象【即User类中的对象user】,所以自动执行全局转换,并调用全局转换属性文件)

 

全局转换属性:

xwork-conversion.priperties

com.shengsiyuan.bean.User=com.shengsiyuan.converter.UserConverter2

#quanju leixingzhuanhuan.zuobian shi leiming

 

 

UserConverter2.java

package com.shengsiyuan.converter;

 

import java.util.Map;

import java.util.StringTokenizer;

 

import org.apache.struts2.util.StrutsTypeConverter;

 

import com.shengsiyuan.bean.User;

 

public class UserConverter2 extends StrutsTypeConverter

{

    //StrutsTypeConverter只是将if toClass ==...else if...进行了封装

    @Override

    public Object convertFromString(Map context, String[] values, Class toClass)

    {//fromstring,即从页面字符串转到后台

       User user = new User() ;

       String value = values[0] ;

       StringTokenizer st = new StringTokenizer(value,";") ;

       user.setUsername(st.nextToken()) ;

       user.setPassword(st.nextToken()) ;

       return user ;

      

    }

 

    @Override

    public String convertToString(Map context, Object o)

    {

       // 后台往字符串转,即后台往页面转

       User user = (User)o ;

       String username = user.getUsername() ;

       String password = user.getPassword() ;

       String userInfo = "username:" +username+"password:"+password ;

       return userInfo ;

    }

 

}

 

之后调用UserAction.java,并调用其中的setUser(user)execute()方法

UserAction.java

package com.shengsiyuan.struts2;

 

import com.opensymphony.xwork2.ActionSupport;

import com.shengsiyuan.bean.User;

 

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" ;

    }

 

}

 

struts.xml部分:

<action name = "userAction" class = "com.shengsiyuan.struts2.UserAction">

       <result name = "SUCCESS">/output.jspresult>

       action>

output.jsp

<%@ page language="java" pageEncoding="gbk"%>

<%@taglib prefix = "s" uri = "/struts-tags" %>

<html>

 

<body>

     <s:property value = "user"/>

     

body>

html>

 

阅读(1477) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~

我要去鸟巢2012-01-21 00:50:52