java struts2入门学习实例--用户注册

 一、用户注册示例

register.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form name="register" action="/struts2/RegisterAction" method="POST">
        <table border="2" align="center">
            <caption>新用户注册</caption>
            <tr>
                <th>用户名:</th>
                <td><input name="username" id="username" type="text" /></td>
            </tr>
            <tr>
                <th>密码:</th>
                <td><input name="password" id="password" type="password" /></td>
            </tr>

            <tr>
                <td colspan="2" align="center"><input type="submit" value="提交"
                    width="120ppx" /></td>
            </tr>

        </table>
    </form>
</body>
</html>
View Code

RegisterAction.java

package com.amos.web.action;

import com.opensymphony.xwork2.ActionSupport;

/** 
* @ClassName: RegisterAction 
* @Description: TODO
* @author: amosli
* @email:amosli@infomorrow.com
* @date Jan 6, 2014 2:31:32 AM  
*/
public class RegisterAction extends ActionSupport {
    private static final long serialVersionUID = -3830387456224903276L;
    private String username;
    private String password;

    public void setUsername(String username) {
        System.out.println("调用 username方法 ");
        this.username = username;
    }

    public void setPassword(String password) {
        System.out.println("调用 password方法 ");
        this.password = password;
    }

    public String register() throws Exception {
        System.out.println("用户名:"+username+"  密码:"+password);
        return null;
    }
}

register_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="register" namespace="/" extends="struts-default">
        <action name="RegisterAction" class="com.amos.web.action.RegisterAction"
            method="register">
        </action>
    </package>
</struts>

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>
    <!-- 加载其他配置文件 -->
    <!--<include file="config/ip_struts.xml"></include>-->

<include file="config/register_struts.xml"></include> </struts>

运行结果如下图所示:

输入用户名:张三,密码:zs,点击提交,控制台输出结果如下:

代码分析:

程序的入口为register.jsp,其中定义了一个表单,其中定义了一个action为"/struts2/RegisterAction",调用方式为POST方式。/struts2项目访问路径,其中RegisterAction是由struts.xml进行加载register_struts.xml中的配置,在register_struts.xml中配置class为com.amos.web.action.RegisterAction,method为register,由此进行反射到RegisterAction.java类。然后调用setter/getter方法,进行数据输出。

二、用户注册(通过GET)

将register.jsp中的method由POST改为GET方式,再进行访问register.jsp,输入张三,zs ,结果如下所示:

为什么出现乱码? 在struts2中,post方示默认会进行转码为utf-8,但get方式却要手工转码.

这里为了使用用户名显示正常,将RegisterAction.java中的setUsername方法更改如下:

public void setUsername(String username) {
        System.out.println("调用 username方法 ");
        try {
            // 将tomcat默认编码转码为utf-8
            username = new String(username.getBytes("ISO8859-1"), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        this.username = username;
    }

tomcat默认的编码是欧洲的ISO8859-1,这里转换一下编码,再重新运行后,结果如下:

显示中文正常了,但是如果将jsp中的调用方式再改为POST时,将会再出现乱码现象,主要是因为已经是utf-8编码的文字再转一次编码就会转错。所以这里要进行判断一下调用方式,代码优化如下所示:

    public void setUsername(String username) {
        System.out.println("调用 username方法 ");
        String method = ServletActionContext.getRequest().getMethod();
        if(method.equals("GET")){
        try {
            // 将tomcat默认编码转码为utf-8
            username = new String(username.getBytes("ISO8859-1"), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        }else if(method.equals("POST")){
            
        }
        this.username = username;
    }

首先取得客户端的请求方式,如果是get那么将首先转码,如是post方式那将什么也不需要做。

 所以在实际开发中建议尽量使用POST提交方式,以避免不必要的麻烦。

三、将用户密码以jsp页面的方式显示

下面将先看效果,再看代码:

RegisterAction.java

package com.amos.web.action;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @ClassName: RegisterAction
 * @Description: TODO
 * @author: amosli
 * @email:amosli@infomorrow.com
 * @date Jan 6, 2014 2:31:32 AM
 */
public class RegisterAction extends ActionSupport {
    private static final long serialVersionUID = -3830387456224903276L;
    private String username;
    private String password;

    public void setUsername(String username) {
        System.out.println("调用 username方法 ");
        String method = ServletActionContext.getRequest().getMethod();
        if(method.equals("GET")){
        try {
            // 将tomcat默认编码转码为utf-8
            username = new String(username.getBytes("ISO8859-1"), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        }else if(method.equals("POST")){
            
        }
        this.username = username;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        System.out.println("调用 password方法 ");
        this.password = password;
    }

    public String register() throws Exception {
        System.out.println("用户名:" + username + "  密码:" + password);
        return "toshow";
    }
}
View Code

register_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="register" namespace="/" extends="struts-default">
        <action name="RegisterAction" class="com.amos.web.action.RegisterAction"
            method="register">
            <result name="toshow" type="dispatcher">/show.jsp</result>
        </action>
    </package>
</struts>

show.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
  用户名:<s:property value="username"/>
 密码:<s:property value="password"/>
</body>
</html>

register.jsp和struts.xml都同上面一样,没有变化。

代码分析:
这里需求是将表单中输入的用户名和密码输出到另一个jsp页面中,那么这里RegisterAction中必须新增加getter方法,return值也不能为null,因为这里要用到result属性,这里配置register_struts.xml文件,将文件内容转发到show.jsp.

这里重点就是show.jsp,这里引入struts-core源码包里的/META-INF/struts-tags.tld,标签来进行调用数据,s是struts的简写,其中value值要和RegisterAction中的两个实例变量名保持一致。

 注意,这里只标签取值仅限于转发的情况下,如果将register-struts.xml中的dispatcher改为redirect那么将取到空值"",如下图所:

原文地址:https://www.cnblogs.com/amosli/p/3506539.html