首页 技术 正文
技术 2022年11月16日
0 收藏 643 点赞 4,746 浏览 3990 个字

用户在做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. 总体项目结构图如下:

Struts2 ModelDriven接口使用

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 显示如下图

Struts2 ModelDriven接口使用

2. 点击提交

显示如下:

Struts2 ModelDriven接口使用

以上为实例截图.

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898