Struts 2 ModelDriven example
2015-09-17 14:59
471 查看
If an Action implements the “
The customer object have to be initialize manually.
http://localhost:8080/Struts2Example/addCustomerAction.action
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/02/79fc58e6aeeac190cad43b6fd8bec1ac)
http://localhost:8080/Struts2Example/customerAction.action
ModelDriven” interface, it gains the extra ability to transfer the form data into the object automatically. See a complete example below :
1. Domain object
A customer object, with setter and getter methods.Customer.java
package com.mkyong.common; public class Customer{ String name; 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; } }
2. Action
Action class, implements theModelDriveninterface, declared the
getModel()method to return the
customer’s object. When the form data is submitted to this action, it will transfers the form data into the customer properties automatically.
The customer object have to be initialize manually.
CustomerAction.java
package com.mkyong.common.action; import com.mkyong.common.Customer; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class CustomerAction extends ActionSupport implements ModelDriven{ //have to initialize it Customer customer = new Customer(); public String execute() throws Exception { return SUCCESS; } public Object getModel() { return customer; } }
3. JSP page
JSP pages for theModelDrivendemonstration.
addCustomer.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 ModelDriven example</h1> <h2>Add Customer</h2> <s:form action="customerAction" > <s:textfield name="name" label="Name" /> <s:textfield name="age" label="Age" value=""/> <s:submit /> </s:form> </body> </html>
success.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 ModelDriven example</h1> <h2>Customer Details</h2> Name : <s:property value="name" /><br /> Age : <s:property value="age" /><br /> </body> </html>
4. struts.xml
Link it all ~<?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="default" namespace="/" extends="struts-default"> <action name="addCustomerAction" class="com.mkyong.common.action.CustomerAction" > <result name="success">pages/addCustomer.jsp</result> </action> <action name="customerAction" class="com.mkyong.common.action.CustomerAction" > <result name="success">pages/success.jsp</result> </action> </package> </struts>
5. Demo
Access the customer form, fill in the form (name : “mkyong”, age ” “123456”) and hits the submit button, the form data (name & age) will be transferred into the customer’s properties (name & age) (match by property name) automatically.http://localhost:8080/Struts2Example/addCustomerAction.action
http://localhost:8080/Struts2Example/customerAction.action
相关文章推荐
- Struts - 标签库
- struts1.0于struts2.0的比较
- Struts2中关于namespace的学习心得
- Struts2----自定义action的学习心得
- HTTP Status 404 - There is no Action mapped for namespace [/] and action name [login!welcome] associ
- struts2.0拦截器(实现未登录的用户不能访问系统的页面)(转)
- 软件开发疑惑解答
- struts2登陆拦截器
- struts2框架原理
- web.xml
- web.xml
- struts2框架原理
- struts2登陆拦截器
- liferay中配置Struts2
- struts2 拦截器和actioninvocation
- Struts2拦截器配置
- stuts2注解
- Struts 2国际化
- Struts2自定义类型转换器分为局部类型转换器和全局类型转换器
- Struts2 错误处理