struts1.2包括相关文件:
struts-config.xml 接收action通过mapping传过来的forward,并通过对应的forward转向不同的页面
Action 处理程序 action可以继承Action,也可以继承DispatchAction
FormBean 用于把封装form表单中的数据
struts1.2工作流程
1,含输入页面,也就是form表单
首先访问form表单页,把form表单页提交到相应的action里,然后在action里通过对应FormBean把form强制转换然后用FormBean接收,
然后把formbean传到相应方法里,然后通过return mapping.findForward("");转到struts-config.xml里对应的forward,然后转向页面,
例如:
1,struts-config.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <data-sources /> <form-beans > <form-bean name="wordsForm" type="com.company.form.WordsForm" /> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings > <action attribute="wordsForm" name="wordsForm" path="/words" scope="request" parameter="method" type="com.company.action.WordsAction"> <forward name="words" path="/form.jsp" /> </action> </action-mappings> <message-resources parameter="com.company.struts.ApplicationResources" /> </struts-config>在struts-config.xml文件里 一个action对应一个formBean
2,WordsForm.java文件如下:
package com.company.form; import java.util.Date; import org.apache.struts.action.ActionForm; public class WordsForm extends ActionForm { private static final long serialVersionUID = -686042683660710968L; private Integer id; private String name; private String content; private Date postTime; private String email; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Date getPostTime() { return postTime; } public void setPostTime(Date postTime) { this.postTime = postTime; } }
3,WordsDao.java接口类
public interface WordsDao { public void insertSave(WordsForm wordsForm); //更新和插入 }
4,WordsDaoImp.java对应的方法
public class WordsDaoImp extends DB implements WordsDao {
@Override
public void insertSave(WordsForm wordsForm) { String sql = ""; if (wordsForm.getId() != null) { sql = "update words set name=?,content=?,postTime=?,email=? where id=?"; this.update( sql, new Object[] { wordsForm.getName(), wordsForm.getContent(), new Date(), wordsForm.getId(),wordsForm.getEmail()}); } else { sql = "insert into words(name,content,id,postTime,email) values(?,?,?,?,?)"; this.insert(sql, new Object[] { wordsForm.getName(), wordsForm.getContent(), wordsForm.getId(), new Date(),wordsForm.getEmail() }); } }
}5,WordsAction.java类
package com.company.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; import com.company.dao.WordsDao; import com.company.dao.imp.WordsDaoImp;; import com.company.form.WordsForm; public class WordsAction extends DispatchAction { WordsDao wordsDao = new WordsDaoImp(); //添加留言 public ActionForward insertWord(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { request.setCharacterEncoding("utf-8"); WordsForm wordsForm = (WordsForm)form; //用WordsForm强制转换form表单 wordsDao.insertSave(wordsForm); //把wordsForm添加到对应方法里 return mapping.findForward("words"); //转向对应mapping中的forward } }
6,输入页,form表单页 form.jsp文件如下:
<form action="words.do?method=insertWord" id="form" method="post"> <table width="398"> <tr> <td height="25" align="left" valign="top"> <input name="name" id="name" type="text" value="您的称呼:" onBlur="if (value ==''){value='您的称呼:'}"> </td> </tr> <tr> <td height="25" align="left" valign="top"> <input name="email" id="email" type="text"onfocus="if (value =='您的邮箱:'){value =''}" onBlur="if (value ==''){value='您的邮箱:'}"> </td> </tr> <tr> <td> <textarea name="content" id="content" overflow:auto" onfocus="if (value =='请输入内容:'){value =''}" onBlur="if (value ==''){value='请输入内容:'}"></textarea></td> </tr></table></form>
然后访问form表单页就可以运行了
2,不含form表单页的流程:
其实大致都是一样的,只不过直接要访问action地址,然后直接处理,比如你要输出内容直接在action里处理,然后用request.setAttribute("name",调用方法);把name传到页面上,然后在相应的页面上用bean标签或者logic标签直接输出就可以了
如果需要把id传值到相应页面上然后访问,那么就要通过超链接里把id传过去
例如:<a href="dongtai.do?method=dongtai&id=<bean:write name="dongtai" property="id" />"><bean:write name="dongtai" property="title" /> </a>
就可以把id传到相应的页面上,然后在action里用String id= request.getParameter("id");接收,然后把id传到相应的方法里,然后就可以了!