struts2 - modeldrive(模型驱动实例)

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值