扣丁學堂盤點JavaEE之Struts1框架輕易入門詳解
2018-01-05 13:33:38
1332瀏覽
JavaEE學習就從最通用的MVC框架Struts開始學習,然后到Hibernate,最后是Spring。對于Struts來說,Struts1和Struts2區(qū)別較大。有部分人認為只需要了解最新的就可以了,但是把最原始的東西多看一下,對我們程序員來說可能收獲更多。畢竟編程,思路是最重要的。
一、什么是Struts
Struts是開源框架(MVC)。使用Struts的目的是為了幫助我們減少在運用MVC設計模型來開發(fā)Web應用的時間。如果我們想混合使用Servlets和JSP的優(yōu)點來建立可擴展的應用,struts是一個不錯的選擇。
二、Struts的常用組件
1.ActionServlet
2.Action
3.ActionForm
4.ActionMapping
5.ActionForward
三、Struts的優(yōu)點
1.基于配置文件的松耦合,Struts中的通常類都被寫在配置文件里面了。如:Action、ActionForm、ActionForward等。如果需要修改一些常用配置的話,只需要修改配置文件就可以了。
2.Formbean(ActionForm)解決了驗證信息的重新顯示需要管理員設置的問題。
3.對數(shù)據(jù)等信息有集中的驗證。
4.支持國際化以及異常信息處理。
四、五大組件介紹
1.ActionServlet
Struts中的最大控制器,是Struts框架的入口,并且封裝了Servlet,被配置在web.xml當中
2.Action
小控制器,處理具體的業(yè)務邏輯
3.ActionForm
和頁面表單對應的一個特殊javabean,負責在頁面和控制器之間傳遞數(shù)據(jù)。同時還提供了集中驗證方法
4.ActionMapping
用來從Struts的配置文件中讀取信息
5.ActionForward
頁面之間的跳轉(zhuǎn)
五、Struts工程創(chuàng)建步驟
1.創(chuàng)建一個Web工程
2.添加Struts的jar包
3.創(chuàng)建一個Struts配置文件struts-config.xml
4.在web.xml添加Struts配置
action
org.apache.struts.action.ActionServlet
config
/WEB-INF/struts-config.xml
action
*.do
5.創(chuàng)建一個LoginForm
packagecom.demo.form;
importorg.apache.struts.action.ActionForm;
publicclassLoginFormextendsActionForm{
privatestaticfinallongserialVersionUID=1L;
privateStringusername;
privateStringpassword;
publicStringgetUsername(){
returnusername;
}
publicvoidsetUsername(Stringusername){
this.username=username;
}
publicStringgetPassword(){
returnpassword;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
}
6.創(chuàng)建一個Action類
packagecom.demo.action;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.apache.struts.action.Action;
importorg.apache.struts.action.ActionForm;
importorg.apache.struts.action.ActionForward;
importorg.apache.struts.action.ActionMapping;
importcom.demo.form.LoginForm;
publicclassDemoActionextendsAction{
publicActionForwardexecute(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse)
throwsException{
//Stringusername=request.getParameter("username");
//Stringpassword=request.getParameter("password");
LoginFormloginform=(LoginForm)form;
Stringusername=loginform.getUsername();
Stringpassword=loginform.getPassword();
if(username!=null&&username.equals("lwh")){
returnmapping.findForward("success");
//request.getRequestDispatcher("/Success.jsp").forward(request,response);
}else{
returnmapping.findForward("failure");
//request.getRequestDispatcher("/Failure.jsp").forward(request,response);
}
//returnnull;
}
}
7.在struts-config.xml中配置Action、ActionForm和ActionForward
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstruts-configPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration1.3//EN""http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-beanname="loginform"type="com.demo.form.LoginForm"></form-bean>
</form-beans>
<action-mappings>
<actionpath="/login"type="com.demo.action.DemoAction"name="loginform">
<forwardname="success"path="/Success.jsp"></forward>
<forwardname="failure"path="/Failure.jsp"></forward>
</action>
</action-mappings>
</struts-config>
8.其他代碼
登陸頁面:
<%@pagelanguage="java"import="java.util.*"pageEncoding="ISO-8859-1"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
</head>
<body>
<formname="loginform"id="form1"action="<%=path%>/login.do"method="post">
<tableborder="0">
<tr>
<td>name:</td>
<td><inputtype="text"name="username"id="username"/></td>
</tr>
<tr>
<td>password:</td>
<td><inputtype="password"name="password"id="password"/></td>
</tr>
<tr>
<tdcolspan="2"align="center"><inputtype="submit"value="submit"/></td>
</tr>
</table>
</form>
</body>
</html>
成功頁面:
<%@pagelanguage="java"import="java.util.*"pageEncoding="ISO-8859-1"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
</head>
<body>
Success!
</body>
</html>
9.運行結果
Success!
以上就是關于扣丁學堂盤點JavaEE視頻教程之Struts1框架輕易入門詳解,最后想要學習JavaEE的朋友大可不必擔心,只要你學好JavaEE游戲開發(fā),想要高薪就業(yè)就不是什么難事??鄱W堂不僅有專業(yè)的老師和課程體系,還有大量的零基礎JavaEE培訓視頻教程供學員觀看學習,想要學習的話就抓緊時間行動吧。Java技術交流群:670348138。
【關注微信公眾號獲取更多學習資料】
查看更多關于“Java開發(fā)資訊”的相關文章>>
標簽:
JavaEE視頻教程
JavaEE培訓
JavaEE開發(fā)工程師
Java培訓