struts2 - modeldrive(模型驅動執行個體)

來源:互聯網
上載者:User
1.匯入jar包

詳情請見:http://blog.csdn.net/zkflame/article/details/51017843


2.new Dynamic Web Project

目錄如下:



3.Customer.java & CustomerAction.java

/* Customer.java */package com.suc.action;public class Customer {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}/* CustomerAction.java */package com.suc.action;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {    private static final long serialVersionUID = 1L;    // 必須初始化    Customer customer = new Customer();    @Override    public String execute() throws Exception {        // TODO Auto-generated method stub        return SUCCESS;    }    @Override    public Customer getModel() {        // TODO Auto-generated method stub        return customer;    }}

4.addCustomer.jsp & success.jsp

/* addCustomer.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"><%@ taglib prefix="s" uri="/struts-tags"%><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Add Customer</title></head><body><h3>Struts 2 ModelDriven example</h3><h3>Add Customer</h3><s:form action="customerAction"><s:textfield name="name" label="姓名" /><s:textfield name="age" label="年齡" value="" /><s:submit value="提交" /></s:form></body></html>/* success.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"><%@ taglib prefix="s" uri="/struts-tags"%><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body>    <h3>Struts 2 ModelDriven example</h3>    <h3>Customer Details</h3>    姓名 :    <s:property value="name" />    <br /> 年齡 :    <s:property value="age" /></body></html>

5.struts.xml & web.xml

/* struts.xml */<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts><constant name="struts.devMode" value="true" /><package name="struts2" namespace="/" extends="struts-default"><action name="addCustomer" class="com.suc.action.CustomerAction"><result>/pages/addCustomer.jsp</result></action><action name="customerAction" class="com.suc.action.CustomerAction"><result name="success">/pages/success.jsp</result></action></package></struts>/* web.xml*/<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee           http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><display-name></display-name><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>

6.display result



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.