本文主要是介绍Action获取页面数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- 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.i18n.encoding" value="UTF-8" /><package name="default" extends="struts-default"><action name="showWords" class="action.ShowWords"><result name="success">/showWords.jsp</result><result name="input">/inputWords.jsp</result></action><action name="showPerson" class="action.ShowPerson"><result name="success">/showPerson.jsp</result></action></package>
</struts>
- 不使用POJO的形式
Action中的代码如下:
package action;import com.opensymphony.xwork2.ActionSupport;public class ShowWords extends ActionSupport{/*** */private static final long serialVersionUID = 5978005658231012920L;private String name;private String words;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getWords() {return words;}public void setWords(String words) {this.words = words;}@Overridepublic String execute() throws Exception {if("".equals(name)){return INPUT;}else{words = "欢迎您" + name;return SUCCESS;}}
}
获取数据的html中Form的代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><title>一个简单的Struts 2应用</title><link rel="stylesheet" type="text/css" href="Style.css"></head><body><center><div><%@ include file="top.html" %></div><h3>一个简单的Struts 2应用</h3><br/><hr/><form name="myForm" action="showWords.action" method="post">请输入您的姓名:<input name="name" type="text"/><br/><input type="submit" name="mySubmit" value="提交"/></form></center>
</body>
</html>
显示数据的html中的Forma信息如下
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><title>Struts 2中使用POJO</title><link rel="stylesheet" type="text/css" href="Style.css"></head><body><center><div><%@ include file="top.html" %></div><h3>使用POJO</h3><br/><hr/></center>姓名:${person.name}<br/>性别:${person.sex}<br/>年龄:${person.age}<br/>住址:${person.address}<br/>
</body>
</html>
- 使用POJO的情况如下
POJO类
package entity;public class Person {private String name;private String sex;private String age;private String address;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}
}
Action中的代码如下
package action;import com.opensymphony.xwork2.ActionSupport;import entity.Person;public class ShowPerson extends ActionSupport{/*** */private static final long serialVersionUID = 5698369708554216333L;private Person person;public Person getPerson() {return person;}public void setPerson(Person person) {this.person = person;}@Overridepublic String execute() throws Exception {return SUCCESS;}}
获得数据的 html中的代码如下
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><title>Struts 2中使用POJO</title><link rel="stylesheet" type="text/css" href="Style.css"></head><body><center><div><%@ include file="top.html" %></div><h3>Struts 2中使用POJO</h3><br/><hr/><s:form action="showPerson"><s:textfield name="person.name" label="姓名"/><s:textfield name="person.sex" label="性别"/><s:textfield name="person.age" label="年龄"/><s:textfield name="person.address" label="住址"/><s:submit value="提交"/></s:form></center>
</body>
</html>
显示数据的Html代码如下
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><title>Struts 2中使用POJO</title><link rel="stylesheet" type="text/css" href="Style.css"></head><body><center><div><%@ include file="top.html" %></div><h3>使用POJO</h3><br/><hr/></center>姓名:${person.name}<br/>性别:${person.sex}<br/>年龄:${person.age}<br/>住址:${person.address}<br/>
</body>
</html>
这篇关于Action获取页面数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!