[Struts2學習筆記] -- 簡單的類型轉換

  接下來學習一下Struts2簡單的類型轉換,Struts2基於ognl.jar實現了簡單類型的數據轉換。好比jsp頁面中的form值與字段值的轉換,下面寫一個例子。css

  一、建立一個jsp頁面,編寫一個form表單模擬登陸,有一個用戶名username和密碼password,在加入一個登陸按鈕。如圖:html

<%@ 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路徑下。java

  二、建立LoginAction,爲了能夠將jsp中表單的自動轉換提取到變量中,須要在action文件裏創建兩個字段,並提供get/set方法用於接收:apache

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類具體代碼以下:less

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類

  三、配置struts2.xml的內容:jsp

<!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的內容

  四、最後當登陸成功時,跳轉到success.jsp頁面。若login.jsp表單中輸入的數據格式有誤,則進行提示,如圖所示,當用戶名或密碼爲空時,提示:ide

  當用戶名密碼錯誤時提示(這裏默認用戶名爲admin,密碼爲password爲正確的信息):post

  當用戶名和密碼正確時,跳轉到success.jsp:學習

  success.jsp的代碼以下:ui

<%@ 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的代碼
相關文章
相關標籤/搜索