天天动画片 > 八卦谈 > STRUTS2result配置和类型转换,通配符配置*,OGNL,注册form表单提交,例子【诗书画唱】

STRUTS2result配置和类型转换,通配符配置*,OGNL,注册form表单提交,例子【诗书画唱】

八卦谈 佚名 2024-01-13 14:35:06

本期导读:STRUTS2result配置和类型转换,返回结果配置的PPT,类型转换的PPT,通配符配置*,OGNL,例子,success叫逻辑视图名,注册form表单提交,讲义



返回结果配置的PPT START







返回结果配置的PPT END

类型转换的PPT START

Struts2提供了非常强大的类型转换机制,只要把HTTP参数命名为合法的OGNL表达式,就可以对表单元素和其他GET/POST的参数使用类型转换机制。同时,Struts2框架为类型转换提供了可扩展性,开发者可以自行定义类型转换器。例如,完成字符串到对象实例的转换

Struts2内部提供大量类型转换器,用来完成数据类型转换问题:

String和boolean、Boolean:完成字符串与布尔值之间的转换

String和char、Character:往常字符串与字符之间的转换

String和int、Integer:完成字符串与整型之间的转换

String和long、Long:完成字符串与长整型值之间的转换

String和double、Double:完成字符串与双精度浮点值的转换

String和float、Float:完成字符串和单精度浮点之间的转换

String和Date:完成字符串和日期类型之间的转换,可以接收yyyy-MM-dd格式字符串

String和数组:可以将多个同名参数,转换到数组中

String和Map、List:支持将数据保存到List或者Map集合






类型转换的PPT END



讲义 START

一、result标签的用法

servlet实现功能:转发和重定向,往页面传递一个字符串response.getWriter().writer();附件上传


type="dispatcher"和type="redirect"的用法

转发:只能跳转到本项目的页面

重定向:可以跳转到外部的网页去


OGNL:对象导航语言


通配符配置*:






讲义 END

例子 START


package com.jy.action;


public class NaviAction {

//http://localhost:8080/J190802/pub/to_indexAc.action

    public String to_index(){

    return "index";

    }

}

package com.jy.action;


public class OneAction {

//http://localhost:8888/J190802/pub/oneAc.action

    public String test(){

    //success叫逻辑视图名

    return "success";

    }

}


package com.jy.action;


public class ProductAction {

//http://localhost:8080/J190802/pro/toAddAc.action

    public String toAdd(){

    //跳转到错误页面

    return "error";

    }

    public String toList(){

    //跳转到错误页面

    return "error";

    }

}


package com.jy.action;


import java.util.Date;


import com.jy.bean.User;


public class PubAction {

private User user;

public User getUser() {

return user;

}

public void setUser(User user) {

this.user = user;

}

public String doReg(){

    System.out.println(user.getName());

    System.out.println(user.isFlag());

    String[]hbs = user.getHobbys();

    for(String hb : hbs) {

    System.out.println(hb);

    }

    return "success";

    }

}

package com.jy.action;


public class TwoAction {

private String target;

public String getTarget() {

return target;

}

public void setTarget(String target) {

this.target = target;

}

//http://localhost:8888/J190802/pub/twoAc.action

    public String gotoPage(){

    //跳转到百度页面www.baidu.com

    target = "jd";

    return "success";

    }

}

package com.jy.bean;


import java.util.Date;


public class User {

private String name;

private Date birthday;

private String sex;

private String[]hobbys;

private boolean flag;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Date getBirthday() {

return birthday;

}

public void setBirthday(Date birthday) {

this.birthday = birthday;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

public String[] getHobbys() {

return hobbys;

}

public void setHobbys(String[] hobbys) {

this.hobbys = hobbys;

}

public boolean isFlag() {

return flag;

}

public void setFlag(boolean flag) {

this.flag = flag;

}

}


<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

    <package name="my" namespace="/pub" extends="struts-default">

        <action name="oneAc" class="com.jy.action.OneAction"

            method="test">

            <result name="success" type="dispatcher">

                <!-- location是不能变的,指定success逻辑视图对应的页面是hello.jsp -->

                <param name="location">/hello.jsp</param>

                <!-- parse是不能变的,是否在页面中允许使用OGNL表达式 -->

                <param name="parse">true</param>

            </result>

        </action>

        <action name="twoAc" class="com.jy.action.TwoAction"

            method="gotoPage">

            <result type="redirect">http://www.${target}.com</result>

        </action>

        <action name="to_*Ac" class="com.jy.action.NaviAction"

            method="to_{1}">

            <result name="{1}">/{1}.jsp</result>

        </action>

    </package>

    <package name="product" namespace="/pro" extends="struts-default">

        <!-- 配置全局的result结果,在product包下的所有action都可以使用这个配置结果 -->

        <global-results>

            <result name="error">/error.jsp</result>

        </global-results>

        <action name="toAddAc" class="com.jy.action.ProductAction"

            method="toAdd">            

        </action>

        <action name="toListAc" class="com.jy.action.ProductAction"

            method="toList">           

        </action>        

    </package>

    <package name="user" namespace="/user" extends="struts-default">

        <action name="regAc" class="com.jy.action.PubAction"

            method="doReg">

            <result>/success.jsp</result>

        </action>

    </package>   

</struts>


<%@ page language="java" contentType="text/html; charset=UTF-8" 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 hreff="<%=basePath%>">

        <title></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">

    </head>

    <body>

        <h1>Hello world</h1>

    </body>

</html>

注册form表单提交:

<%@ page language="java" contentType="text/html; charset=UTF-8" 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 hreff="<%=basePath%>">

        <title></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">

        

    </head>

    <body>

        <form action="user/regAc.action?user.flag=true" method="post">

            <label>姓名</label><input type="text" name="user.name"/>

            <br>

            <label>生日</label><input type="text" name="user.birthday"/>

            <br>

            <label>性别</label>

            <input type="radio" name="user.sex" value="男"/>男

            <input type="radio" name="user.sex" value="女"/>女

            <br>

            <label>爱好</label>

            <input type="checkbox" name="user.hobbys" value="读书"/>读书

            <input type="checkbox" name="user.hobbys" value="街舞"/>街舞

            <input type="checkbox" name="user.hobbys" value="电影"/>电影

            <br>

            <input type="submit" value="注册"/>

        </form>

    </body>

</html>



例子 END





本文标题:STRUTS2result配置和类型转换,通配符配置*,OGNL,注册form表单提交,例子【诗书画唱】 - 八卦谈
本文地址:www.ttdhp.com/article/45723.html

天天动画片声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
扫码关注我们