Struts2的类型转换

Terwer Struts2评论24字数 5421阅读18分4秒阅读模式

使用标签

注意Struts2使用标签库找不到URI:[struts-tags]的taglib

问题解决文章源自浅海拾贝-https://blog.terwergreen.com/struts2-type-conversion-pjcr7.html

新建 login2.jsp​ 文件,内容如下:文章源自浅海拾贝-https://blog.terwergreen.com/struts2-type-conversion-pjcr7.html

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Struts2.3 Login2</title>
</head>
<body>
    <s:form action="login">
        <s:textfield name="username" label="username"></s:textfield>
        <s:textfield name="password" label="password"></s:textfield>
        <s:submit label="submit"></s:submit>
    </s:form>
</body>
</html>

查看结果:文章源自浅海拾贝-https://blog.terwergreen.com/struts2-type-conversion-pjcr7.html

Struts2的类型转换文章源自浅海拾贝-https://blog.terwergreen.com/struts2-type-conversion-pjcr7.html

在 Action 中使用 ActionSupport

新增页面

login3.jsp文章源自浅海拾贝-https://blog.terwergreen.com/struts2-type-conversion-pjcr7.html

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Struts2.3 Login3</title>
</head>
<body>
    <s:form action="login3">
        <s:textfield name="username" label="username"></s:textfield>
        <s:textfield name="password" label="password"></s:textfield>
        <s:submit label="submit"></s:submit>
    </s:form>
</body>
</html>

LoginAction3.java文章源自浅海拾贝-https://blog.terwergreen.com/struts2-type-conversion-pjcr7.html

package space.terwer;

import org.apache.commons.lang3.StringUtils;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction3 extends ActionSupport{
    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;
    }

    @Override
    public void validate() {
        if(StringUtils.isBlank(this.getUsername())) {
            this.addFieldError("username", "username required");
        }
        if(StringUtils.isBlank(this.getPassword())) {
            this.addFieldError("password", "password required");
        }
    }

    public String execute() {
        return "success";
    }
}

修改 struts.xml 添加校验跳转 input

struts.xml文章源自浅海拾贝-https://blog.terwergreen.com/struts2-type-conversion-pjcr7.html

<!-- 使用ActionSupport -->
<action name="login3" class="space.terwer.LoginAction3">
    <result name="success">/result.jsp</result>
    <result name="input">/login3.jsp</result>
</action>

效果

Struts2的类型转换文章源自浅海拾贝-https://blog.terwergreen.com/struts2-type-conversion-pjcr7.html

业务验证

LoginAction2.java​ 的 execute()​ 方法添加验证逻辑文章源自浅海拾贝-https://blog.terwergreen.com/struts2-type-conversion-pjcr7.html

public String execute() {
    if("hello".equals(this.getUsername().trim()) && "world".equals(this.getPassword().trim())){
        return "success";
    }

    this.addFieldError("username", "username or password error");
    return "failer";
}

struts.xml​ 加入跳转逻辑文章源自浅海拾贝-https://blog.terwergreen.com/struts2-type-conversion-pjcr7.html

<!-- 使用ActionSupport -->
<action name="login3" class="space.terwer.LoginAction3">
    <result name="success">/result.jsp</result>
    <result name="input">/login3.jsp</result>
    <result name="failer">/login3.jsp</result>
</action>

效果文章源自浅海拾贝-https://blog.terwergreen.com/struts2-type-conversion-pjcr7.html

Struts2的类型转换文章源自浅海拾贝-https://blog.terwergreen.com/struts2-type-conversion-pjcr7.html

注意:result.jsp​ 里面的两种写法等价。文章源自浅海拾贝-https://blog.terwergreen.com/struts2-type-conversion-pjcr7.html

username:${requestScope.username}<br /> 
password:${requestScope.password }
username:<%=request.getParameter("username") %><br /> 
password:<%=request.getParameter("password") %>

类型转换深入

新建一个 input.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Struts2.3 convert test</title>
</head>
<body>

    <h3 style="color:red;">使用逗号将点的两个坐标分隔开</h3>
    <s:form action="pointConverter">
        <s:textfield name="point" label="point"></s:textfield>
        <s:textfield name="age" label="age"></s:textfield>
        <s:textfield name="username" label="username"></s:textfield>
        <s:textfield name="date" label="birthday"></s:textfield>

        <s:submit value="Submit"></s:submit>
    </s:form>
</body>
</html>

Struts2的类型转换文章源自浅海拾贝-https://blog.terwergreen.com/struts2-type-conversion-pjcr7.html

Point 对象

新建一个 bean​ 为 Point.java文章源自浅海拾贝-https://blog.terwergreen.com/struts2-type-conversion-pjcr7.html

package space.terwer.bean;

public class Point {
    private Integer x;
    private Integer y;

    public Integer getX() {
        return x;
    }

    public void setX(Integer x) {
        this.x = x;
    }

    public Integer getY() {
        return y;
    }

    public void setY(Integer y) {
        this.y = y;
    }
}

OGNL 官网

https://commons.apache.org/proper/commons-ognl/文章源自浅海拾贝-https://blog.terwergreen.com/struts2-type-conversion-pjcr7.html

新的官网文章源自浅海拾贝-https://blog.terwergreen.com/struts2-type-conversion-pjcr7.html

https://github.com/apache/commons-ognl文章源自浅海拾贝-https://blog.terwergreen.com/struts2-type-conversion-pjcr7.html

新建一个类型转换器类 PointConverter

package space.terwer.converter;

import java.util.Map;

import ognl.DefaultTypeConverter;
import space.terwer.bean.Point;

public class PointConverter extends DefaultTypeConverter {

    @Override
    public Object convertValue(Map context, Object value, Class toType) {
        if (Point.class == toType) {
            Point point = new Point();

            String[] str = (String[]) value;
            String[] paramValues = str[0].split(",");

            Integer x = new Integer(paramValues[0]);
            Integer y = new Integer(paramValues[1]);

            point.setX(x);
            point.setY(y);

            return point;
        }

        if (String.class == toType) {
            Point point = (Point) value;

            int x = point.getX();
            int y = point.getY();

            String result = "[x=" + x + ", y=" + y + "]";

            return result;
        }

        return null;
    }

}

新建一个 PointAction 处理类

package space.terwer.action;

import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

import space.terwer.bean.Point;

public class PointAction extends ActionSupport {
    private Point point;
    private Integer age;
    private String username;
    private Date date;

    public Point getPoint() {
        return point;
    }

    public void setPoint(Point point) {
        this.point = point;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }
}

struts.xml 添加处理流程配置:

<action name="pointConverter" class="space.terwer.action.PointAction">
    <result name="success">/output.jsp</result>
</action>

新增转换器配置

必须在当前 Action 同级目录新建 Action类名-conversion.properties​ ,例如:文章源自浅海拾贝-https://blog.terwergreen.com/struts2-type-conversion-pjcr7.html

PointAction-conversion.properties

内容为 需要转换的属性名称​ 和 转换器全路径​ ,例如本例子内容如下:文章源自浅海拾贝-https://blog.terwergreen.com/struts2-type-conversion-pjcr7.html

point=space.terwer.converter.PointConverter

重启服务器。文章源自浅海拾贝-https://blog.terwergreen.com/struts2-type-conversion-pjcr7.html

特别指出:转换器文件前面必须是Action类名,后缀必须是:文章源自浅海拾贝-https://blog.terwergreen.com/struts2-type-conversion-pjcr7.html

-conversion.properties

Struts2的类型转换文章源自浅海拾贝-https://blog.terwergreen.com/struts2-type-conversion-pjcr7.html

output.jsp 显示页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>  

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Point result</title>
</head>
<body>

    point:<s:property value="point" /><br/>
    age:<s:property value="age" /><br/>
    username:<s:property value="username" /><br/>
    date:<s:property value="date" />

</body>
</html>

查看效果

Struts2的类型转换文章源自浅海拾贝-https://blog.terwergreen.com/struts2-type-conversion-pjcr7.html

相关文章
历史上的今天
10月
30
  • 扫码加我微信
  • 验证消息请输入:来自你的博客
  • weinxin
  • 我的微信公众号
  • 微信扫一扫与我交流吧
  • weinxin
Terwer
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: