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表达式语言,其用法暂略。

你可能感兴趣的:(DAO,C++,c,struts,C#)