Struts2 ModelDriven接口使用
用户在做http请求时一般都有两种方式:get和post方式.get方式用来获取查询相关信息,既向服务器获得信息,而post方式用来更新信息既向服务器提交数据.通常情况下,用get方式向服务器获取信息是附带的信息量都比较少,可以用servlet API来一个一个获取,但是当post方式提交数据时,往往数据量都比较大,如果用servlet API方式获取数据代码过于臃肿,维护和修改比较复杂降低了程序员工作效率.strtus2框架提供了ModelDrvem接口,对于实现接口Action来说只需定义Model,struts2框架就会自动将用户提交的http信息赋给相应的model.
以下已实例的方式说明,项目包括:action类LoginAction.java,model类PersonModel.java,struts.xml,web.xml文件,index.jsp,success.jsp文件.
1. 建立WEB工程 Struts2ModelDrivenDemo,添加相关的动态库和开发包,请参照前面介绍
2. 总体项目结构图如下:
3. 项目的代码介绍
Action类 LoginAction代码如下:
package com.northeasttycoon.action; import com.northeasttycoon.model.PersonModel; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; /** @author NorthEastTycoon */ public class LoginAction extends ActionSupport implements ModelDriven<PersonModel>{ private static final long serialVersionUID = 1L; private PersonModel _personModel = new PersonModel(); @Override public PersonModel getModel() { return _personModel; } public String execute() throws Exception{ ActionContext _context = ActionContext.getContext(); _context.put("person", _personModel); return SUCCESS; } }
model类PersonModel.java
package com.northeasttycoon.model; /** @author NorthEastTycoon */ public class PersonModel { private String name; // 名字 private String degree;// 等级 private String age;//年龄 private String company;//公司 private String address;//地址 private String telephone;//电话 public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDegree() { return degree; } public void setDegree(String degree) { this.degree = degree; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getCompany() { return company; } public void setCompany(String company) { this.company = company; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } }
struts.xml
<?xml version="1.0" encoding="UTF-8"?><!-- northeasttycoon --> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> <action name="person" class="com.northeasttycoon.action.LoginAction"> <result name="success">/success.jsp</result> </action> </package> </struts>
web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!-- northeasttycoon --> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Struts 2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
index.jsp
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="tycoon" uri="/struts-tags"%> <html> <head> <title>Hello World</title> </head> <body> <center> <tycoon:form action="person"> <tycoon:textfield lable="name" name="name"/> <tycoon:textfield lable="degree" name="degree"/> <tycoon:textfield lable="age" name="age"/> <tycoon:textfield lable="company" name="company"/> <tycoon:textfield lable="address" name="address"/> <tycoon:textfield lable="telephone" name="telephone"/> <tycoon:submit/> </tycoon:form> </center> </body> </html>
success.jsp
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="tycoon" uri="/struts-tags"%> <html> <head> <title>Hello World</title> </head> <body> <center> <tycoon:property value="#person.name"/></br> <tycoon:property value="#person.degree"/></br> <tycoon:property value="#person.age"/></br> <tycoon:property value="#person.company"/></br> <tycoon:property value="#person.address"/></br> <tycoon:property value="#person.telephone"/></br> </center> </body> </html>
执行的结果截图:
1. 浏览器输入:http://localhost:8080/Struts2ModelDrivenDemo 显示如下图
2. 点击提交
显示如下:
以上为实例截图.