Java Hour 33 Weather ( 6 )

时间:2021-01-12 05:13:23

有句名言,叫做10000小时成为某一个领域的专家。姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧。

Hour 33

CURD 之 Create

首先是Eclipse 排序的功能找到了 - -

Sort Member

从Model开始

package org.apache.struts.helloworld.model;

public class Person {
    private String firstName;
    private String lastName;
    private String email;
    private int age;

    public int getAge() {
        return age;
    }

    public String getEmail() {
        return email;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

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

    public void setEmail(String email) {
        this.email = email;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String toString() {
        return "First Name: " + getFirstName() + " Last Name:  " + getLastName() + " Email:      " + getEmail() + " Age:      " + getAge();
    }
}

Register 的View

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Register</title>
</head>
<body>
    <h3>Register for a prize by completing this form.</h3>

    <s:form action="register">
        <s:textfield name="personBean.firstName" label="First name" />
        <s:textfield name="personBean.lastName" label="Last name" />
        <s:textfield name="personBean.email" label="Email" />
        <s:textfield name="personBean.age" label="Age" />
        <s:submit />
    </s:form>
</body>
</html>

这里有熟悉的tag, s:from, s:textfield, s:submit

Controller Class 这个最关键了

package org.apache.struts.register.action;

import org.apache.struts.register.model.Person;
import com.opensymphony.xwork2.ActionSupport;

public class Register extends ActionSupport {

    private static final long serialVersionUID = 1L;
    private Person personBean;

    @Override
    public String execute() throws Exception {
        // call Service class to store personBean's state in database
        return SUCCESS;
    }

    public Person getPersonBean() {
        return personBean;
    }

    public void setPersonBean(Person person) {
        personBean = person;
    }
}

一个简单的Success View

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Registration Successful</title>
</head>
<body>
    <h3>Thank you for registering for a prize.</h3>

    <p>
        Your registration information:
        <s:property value="personBean" />
    </p>

    <p>
        <a href="<s:url action='index' />">Return to home page</a>.
    </p>

</body>
</html>

这里就两个tag s:property, s:url

struts.xml 配置映射

<action name="hello"
            class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
            <result name="success">/HelloWorld.jsp</result>
        </action>

        <action name="register" class="org.apache.struts.register.action.Register"
            method="execute">
            <result name="success">/thankyou.jsp</result>
        </action>

Result

Java Hour 33 Weather ( 6 )