Struts2模型驱动封装

  • 平常写一下小东西,用到模型驱动封装,简直方便的不要不要的。

  • action页面 
package Action;

import entity.Product;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class Product_Action extends ActionSupport implements ModelDriven<Product>{
    private Product product=new Product();
    @Override
    public Product getModel() {
        return product;
    }
    public String execute(){
        HttpServletRequest request= ServletActionContext.getRequest();
        HttpSession session=request.getSession();
        session.setAttribute("product",product);
        return SUCCESS;
    }
}
  •  两个jsp页面
<%@ page import="entity.Product" %><%--
  Created by IntelliJ IDEA.
  User: jdq8576
  Date: 2018/11/6
  Time: 10:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Save Product</title>
    <style type="text/css">
        /*@import url(../css/main.css);*/
        #global {
            text-align: left;
            border: 1px solid #dedede;
            background: saddlebrown;
            width: 560px;
            padding: 20px;
            margin: 100px auto;
        }
    </style>
</head>
<body>
<%
    Product product=(Product)session.getAttribute("product");
    String name=product.getName();
    String description=product.getDescription();
    String price=product.getPrice();
%>
<div id="global">
    <h4>The product has been saved.</h4>
    <p>
        <h5>Details:</h5>
        Product Name: ${name}<br/>
        Description: ${description}<br/>
        Price: $${price}
    </p>
</div>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: jdq8576
  Date: 2018/11/6
  Time: 9:23
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Add Product Form</title>
    <style type="text/css">
        #global {
            text-align: left;
            border: 1px solid #dedede;
            background: saddlebrown;
            width: 560px;
            padding: 20px;
            margin: 100px auto;
        }

        form {
            font: 100% verdana;
            min-width: 500px;
            max-width: 600px;
            width: 560px;
        }

        form fieldset {
            border-color: saddlebrown;
            border-width: 3px;
            margin: 0;
        }

        legend {
            font-size: 1.3em;
        }

        form label {
            width: 250px;
            display: block;
            float: left;
            text-align: right;
            padding: 2px;
        }

        #buttons {
            text-align: right;
        }
    </style>
</head>
<body>
<div id="global">
    <form action="actionSupport.action" method="post">
        <fieldset>
            <legend>Add a product</legend>
            <p>
                <label for="name">Product Name:</label>
                <input type="text" id="name" name="name" tabindex="1">
            </p>
            <p>
                <label for="description">Description:</label>
                <input type="text" id="description" name="description" tabindex="2">
            </p>
            <p>
                <label for="price">Price:</label>
                <input type="text" id="price" name="price" tabindex="3">
            </p>
            <p id="buttons">
                <input id="reset" type="reset" tabindex="4">
                <input id="submit" type="submit" tabindex="5" value="Add Product">
            </p>
        </fieldset>
    </form>
</div>
</body>
</html>
  •  Product类
package entity;

public class Product {
    private String name,description,price;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }
}
  • Struts核心文件:struts.xml
<?xml version="1.0" encoding="UTF-8"?>

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

<struts>
    <package name="_1021" extends="struts-default" namespace="/">
        <action name="actionSupport" class="Action.Product_Action" method="execute">
            <result name="success">/jsps/ProductDetails.jsp</result>
        </action>
    </package>
</struts>

猜你喜欢

转载自blog.csdn.net/jdq8576/article/details/83782455