现在的位置: 首页 > 综合 > 正文

浅析Struts 1.2

2019年03月08日 ⁄ 综合 ⁄ 共 4101字 ⁄ 字号 评论关闭
浅析Struts <wbr>1.2浅析Struts <wbr>1.2浅析Struts <wbr>1.2-->>最近重温了一下Struts 1.2,和Struts 2.x相比,感觉各有优劣,估计是Struts 2
用得比较上手,个人还是比较喜欢用Struts 2 浅析Struts <wbr>1.2,估计这也是很多的想法吧!!

Struts
1.2是一个基于Servlet的一个框架,为入侵式设计:

好吧,说了一些废话,直接上图--->>
先看一下Struts
1.2的执行流程吧(图为公司原创):
浅析Struts <wbr>1.2
是不是初略的看了下Struts
1.2的流程,感觉很简单!--->(相对于struts 2):
好吧,我总结了一下:
1-->.服务器启动后,根据web.xml加载ActionServlet读取struts-config.xml文件内容到内存。
2-->.以登录为例:第一次进login.jsp会先实例化Form、把默认值(String默认为空字符串,整型默认为0)赋给表单元素。
3-->.输入用户名密码提交表单、提交到action属性的login.action,通过ActionServlet读struts-config.xml文件找到
action下的path属性找到.action,通过name属性找form-beans中的form-bean的name属性得到ActionForm的包名类名,先实例化form,把表单的值填充给form,调用form的validate方法验证、ActionErrors返回null表示验证通过,否则失败返回input指定的页面.验证通过会实例化Action,执行Action的execute方法。
Struts
1.2作为当年最为流行的 MVC框架,优点也十分明显:

---》:Taglib和页面导航。Taglib是Struts的标记库,灵活运用,能大大提高开发效率。



了解了原理,我们来着手搞个案例:

浅析Struts <wbr>1.2
你没有看错,这是我这个小列子的框架图。

进入主题:
第一步:导入jar库:
单击项目节点--->>右键-->>Myeclipse--->>Add
Struts comp....:后面不用说了


第二步:进行Form,Action,Jsp视图的关联:(不做详细介绍)
右键-->new ->在other中找到Struts
1.2 Form,Action &
JSP-->next
如图:浅析Struts <wbr>1.2

如图所示之后:next

浅析Struts <wbr>1.2

之后:就是贴代码了--->>
Form文件中:
public class MathForm extends ActionForm {
private String oper;
public String getOper() {
return oper;
}
public void setOper(String oper) {
if ("doAdd".equals(oper)) {
oper = "+";
}
if ("doSubtract".equals(oper)) {
oper = "-";
}
if ("doCheng".equals(oper)) {
oper = "*";
}
if ("doChu".equals(oper)) {
oper = "/";
}
this.oper = oper;
}
private MatchEntity match = new MatchEntity();
public MatchEntity getMatch() {
return match;
}
public void setMatch(MatchEntity match) {
this.match = match;
}
@SuppressWarnings("deprecation")
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if ("/".equals(oper)) {//除数为零时输出
if (match.getNum2() == 0) {
errors.add("match.num2", new ActionError("chu"));
}
}
return errors;
}
Action文件中 --->>
package com.lovesmile.oa.web.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.lovesmile.oa.biz.MatchBiz;
import com.lovesmile.oa.web.form.MathForm;
public class MathAction extends DispatchAction {
MatchBiz matchBiz=new MatchBiz();
public ActionForward doAdd(ActionMapping mapping, ActionForm
form,
HttpServletRequest request, HttpServletResponse response)
{
MathForm mathForm = (MathForm) form;// TODO Auto-generated
method stub
Integer result = matchBiz.add(mathForm.getMatch());
request.setAttribute("result", result);
return mapping.findForward("success");
}
public ActionForward doSubtract(ActionMapping mapping,
ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
MathForm mathForm = (MathForm) form;// TODO Auto-generated
method stub
Integer result = matchBiz.subtract(mathForm.getMatch());
request.setAttribute("result", result);
return mapping.findForward("success");
}
public ActionForward doCheng(ActionMapping mapping, ActionForm
form,
HttpServletRequest request, HttpServletResponse response)
{
MathForm mathForm = (MathForm) form;// TODO Auto-generated
method stub
Integer result = matchBiz.cheng(mathForm.getMatch());
request.setAttribute("result", result);
return mapping.findForward("success");
}
public ActionForward doChu(ActionMapping mapping, ActionForm
form,
HttpServletRequest request, HttpServletResponse response)
{
MathForm mathForm = (MathForm) form;// TODO Auto-generated
method stub
Integer result = matchBiz.chu(mathForm.getMatch());
request.setAttribute("result", result);
return mapping.findForward("success");
}
}
界面代码--->>:
<%@ page language="java"
pageEncoding="UTF-8"%>
<%@ taglib
uri="http://struts.apache.org/tags-bean"
prefix="bean"%> 
<%@ taglib
uri="http://struts.apache.org/tags-html"
prefix="html"%>
 
<html> 
<head>
<title>JSP for MathForm
form</title>
<script
type="text/javascript">
function opera(opr){
document.forms[0].elements["oper"].value=opr;
document.forms[0].submit();
}
</script>
</head>
<body>
<form action="math.do" >
<input type="hidden"
name="oper"/>
第一个数:<input type="text" name="match.num1"
/> <br/>
第二个数:<input type="text" name="match.num2"
/><br/>
<button type="button"
onclick="opera('doAdd')">加</button>|<button
type="button"
onclick="opera('doSubtract')">减</button>|<button
type="button"
onclick="opera('doCheng')">乘</button>|<button
type="button"
onclick="opera('doChu')">除</button>
</form>
</body>
</html>
好吧,今天就到这了,详情-->>奥斯科技

抱歉!评论已关闭.