Structs 开发入门笔记二

Structs.xml;-----------制定的框架和流程。
<package name="default" namespace="/" extends="struts-default">
<action name="aCheck" class="AccountCheck">
<result name="toIndex2">/index2.jsp</result>
</action>
</package>

<package name="后台包" namespace="/" extends="struts-default">
<action name="页面form(1)" class="后台action类(2)">
<result name="后台action结果(3)">前台跳转页面(4)</result>
</action>
</package>

页面--------------展现数据
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %> //特定structs标签
<html>
<body>
<s:form action="aCheck"> //页面form action(1)
<s:textfield name="name" value="wang"></s:textfield> //页面后台变量
<s:textfield name="password" value="123"></s:textfield> //页面后台变量
<s:submit></s:submit>
</s:form>
</body>
</html>

后台----处理逻辑,传出结果。
import com.opensymphony.xwork2.ActionSupport;
public class AccountCheck extends ActionSupport{ //后台Action类(2)
private String name; //后台页面变量
private String password; //后台页面变量
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute(){
System.out.println(name+password);
return "toIndex2"; //后台结果,返到页面(3)
}
}


-------------------------------后台如何处理数据------------------------------


数据处理action的“三板斧”为:
(1) 获取当前应用上下文;
(2) 获取实体类的DAO对象实例;
(3) 使用DAO对象操作数据;
特别提醒:在使用DAO对象操作数据时,经常使用Java的集合类对象,例如List等。
EmployeeList.java核心代码为:
public class EmployeeList extends ActionSupport {
public String execute() {
ApplicationContext ct = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());//(1) 获取当前应用上下文;
EmployeeDAO dao = EmployeeDAO.getFromApplicationContext(ct);//(2) 获取实体类的DAO对象实例;
List<Employee> list = dao.findAll();//(3) 使用DAO对象操作数据;
int n = list.size();
for (int i = 0; i < n; i++) {
Employee c = list.get(i);
String name = c.getFirstname();
String lastname = c.getLastname();
System.out.println(name+"."+lastname);
}
return "toList";
}
}


数据操纵的四板斧:
(1) 依次获取应用上下文、DAO对象和当前会话对象;
(2) 启动事务;
(3) 数据操纵;
(4) 提交事务

Employee em;
public String execute() {
ApplicationContext ct = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());//(1) 依次获取应用上下文
EmployeeDAO dao = EmployeeDAO.getFromApplicationContext(ct);//(1) 依次获取应用上下文、DAO对象
Session s = dao.getSessionFactory().openSession();//(1) 依次获取应用上下文、DAO对象和当前会话对象;
Transaction tx = s.beginTransaction();//(2) 启动事务;
try {
dao.attachDirty(em);//(3) 数据操纵;
tx.commit();//(4) 提交事务
} catch (Exception e) {
e.printStackTrace();
}
return "toList";
}


------------------------------------------------后台与页面数据交互----------------------


<s:text name="password"/><hr/>
<s:property value="password" /><hr/>
${password}<hr/>
运行一下试试。可以看到,三种方法都可以将输入的password显示出来。其中前两种为Struts标签形式,${password}的写法是OGNL表达式语言,其用法暂略。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值