[Struts2学习笔记] -- 简单的类型转换

  接下来学习一下Struts2简单的类型转换,Struts2基于ognl.jar实现了简单类型的数据转换。比如jsp页面中的form值与字段值的转换,下面写一个例子。

  1、创建一个jsp页面,编写一个form表单模拟登录,有一个用户名username和密码password,在加入一个登录按钮。如图:

<%@ 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 'index.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:form name="login" method="post" action="login" namespace="/">
        <s:textfield label="username" name="username"></s:textfield>
        <s:textfield label="password" name="password"></s:textfield>
        <s:submit value="sgin in"></s:submit>
    </s:form>
  </body>
</html>
login.jsp代码

  jsp代码中使用了struts2的标签库,标签库的.tld文件在struts2-core-2.3.24.1.jar/META-INF路径下。

  2、创建LoginAction,为了可以将jsp中表单的自动转换提取到变量中,需要在action文件里建立两个字段,并提供get/set方法用于接收:

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

private String username;
private String password;

  其超类ActionSupport提供了验证功能,需要重写超类的validate()方法,对表单中的数据进行校验,并调用addFieldError()方法记录错误信息,当验证失败,会返回INPUT作为Result,所以需要在struts2.xml中配置返回值为input的result节点,LoginAction类具体代码如下:

package cn.net.bysoft.lesson2;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

    /**
     * 
     */
    private static final long serialVersionUID = -549393995867033046L;

    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        if ("admin".equals(getUsername()) && "password".equals(getPassword())) {
            return SUCCESS;
        } else {
            addFieldError("username",
                    "Sorry,wrong username or password,please login again");
            return INPUT;
        }
    }

    @Override
    public void validate() {
        // TODO Auto-generated method stub
        if (getUsername().length() == 0) {
            addFieldError("username", "username is required");
        }
        if (getPassword().length() == 0) {
            addFieldError("password", "password is required");
        }
    }

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

    private String username;
    private String password;
}
LoginAction类

  3、配置struts2.xml的内容:

<!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_3_24_1" extends="struts-default">
        <action name="helloWorld" class="cn.net.bysoft.lesson1.HelloWorldAction">
            <result>lesson1/hello.jsp</result>
        </action>
        <action name="login" class="cn.net.bysoft.lesson2.LoginAction">
            <result name="success">lesson2/success.jsp</result>
            <result name="input">lesson2/login.jsp</result>
        </action>
    </package>
</struts>
struts2.xml的内容

  4、最后当登录成功时,跳转到success.jsp页面。若login.jsp表单中输入的数据格式有误,则进行提示,如图所示,当用户名或密码为空时,提示:

  当用户名密码错误时提示(这里默认用户名为admin,密码为password为正确的信息):

  当用户名和密码正确时,跳转到success.jsp:

  success.jsp的代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ 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 'success.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>
    username: <s:property value="username"/><br>
    password: <s:property value="password"/><br>
  </body>
</html>
success.jsp的代码

 

posted @ 2016-01-20 21:18  TheBlackPearl  阅读(202)  评论(0编辑  收藏  举报