知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營銷的便利,運(yùn)營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
您當(dāng)前位置>首頁 » 新聞資訊 » 網(wǎng)站建設(shè) >
Eclipse上開發(fā)簡單Web項(xiàng)目
發(fā)表時(shí)間:2018-1-2
發(fā)布人:葵宇科技
瀏覽次數(shù):65
本文是在Eclipse已經(jīng)完成tomcat部署基礎(chǔ)上進(jìn)行講述基本操作的,如果對(duì)tomcat部署相關(guān)知識(shí)有任何疑問的,可看我之前發(fā)表的有關(guān)tomcat部署相關(guān)的博文。
1.新建一個(gè)動(dòng)態(tài)web項(xiàng)目
如圖所示,直接在eclipse的project explorer空白處右鍵單擊,選擇new 然后點(diǎn)project web project,然后按在下圖輸入項(xiàng)目名字點(diǎn)finish即可。
新建完成的項(xiàng)目結(jié)構(gòu)如下圖所示:
2.部署struts開發(fā)包
其中有一種是簡單的復(fù)制方法:將需要的jar包從struts2.2.3.1/lib中復(fù)制到WebContent/WEB-INF/lib文件夾中。 此處介紹一種部署方法,部署之后,每次建立項(xiàng)目不需要每次都復(fù)制粘貼。如下圖所示,右鍵單擊WebContent中WEB-INF文件夾下的lib文件夾,選擇Build path->configure Build path。點(diǎn)擊configure Build path之后按下圖操作:
首先,如果libraries里沒有struts2,則點(diǎn)右邊的Add Library-->User libraries,假如仍然沒有next,然后點(diǎn)new,新建一個(gè)library,并命名,然后點(diǎn)Add JARs,該步驟則是定位到struts安裝包下的lib文件夾下相應(yīng)的jars文件。全部部署之后,MyWeb項(xiàng)目下會(huì)出現(xiàn)一個(gè)Struts2的library,struts部署結(jié)束。如下圖所示:
3.編寫web配置文件web.xml
該配置文件大致沒有什么其它不同,可以從其它web項(xiàng)目拷貝,只需要知道,該web配置文件需要配置一個(gè)struts的核心filter,而且語句一般固定,可以從其它地方復(fù)制。另外需要關(guān)聯(lián)一個(gè)主jsp文件,該jsp頁面則相當(dāng)于網(wǎng)頁的首頁,如下圖所示,index.jsp就是該項(xiàng)目的主頁面。 web.xml:<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Struts</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4.編寫控制器文件struts.xml
struts文件主要用于將action與jsp頁面相關(guān)聯(lián)起來。比如有個(gè)action是welcome,它相對(duì)應(yīng)的jsp頁面是hello.jsp,則需要在struts文件中進(jìn)行配置,將action名welcome,jsp頁面,相對(duì)應(yīng)的action函數(shù)進(jìn)行關(guān)聯(lián)起來。至于action函數(shù),默認(rèn)的話則是excute方法,若是其他方法,則應(yīng)該在<action.../>中聲明method變量,而welcome則是name值,而jsp頁面則在<result.../>語句中表示清楚。 struts.xml:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="hello" class="ab.TestAction">
<result>/success.jsp</result>
</action>
<action name="reject" class="ab.TestAction" method="reject">
<result name="error">/reject.jsp</result>
</action>
</package>
</struts>
5.編寫jsp文件
這里jsp文件分為兩種,一種是主jsp文件,即前面講述的index.jsp,這個(gè)文件是主頁面,并給其他頁面提供入口,給了其他頁面與相關(guān)action相關(guān)聯(lián)的切入點(diǎn)。 index.jsp:<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="S" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<S:a action="hello">hello</S:a>
<br>
<S:a action="reject">reject</S:a>
</body>
</html>
index.jsp中則在<body..../>中部署了兩個(gè)action,分別為hello,reject。這兩個(gè)名字也將在主頁面呈現(xiàn)。如下圖所示;
正是有了以上兩個(gè)action,則接下來需要編寫相應(yīng)的jsp文件,如下代碼為兩個(gè)jsp文件: success.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:property value="helo" />
<br>
<s:property value="res" />
</body>
</html>
reject.xml:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:property value="sorry" />
</body>
</html>
6.開發(fā)用于后臺(tái)處理的Action
Action的編寫十分重要,需要用于與其相對(duì)應(yīng)的jsp頁面相關(guān)聯(lián),通過Action函數(shù)的處理,呈現(xiàn)出相應(yīng)的頁面。 TestAction:package ab;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String helo;
private String res;
private String sorry;
public String getHelo() {
return helo;
}
public String getRes() {
return res;
}
public String getSorry() {
return sorry;
}
public void setHelo(String helo) {
this.helo = helo;
}
public void setRes(String res) {
this.res = res;
}
public void setSorry(String sorry) {
this.sorry = sorry;
}
@Override
public String execute() throws Exception {
helo = "hello,my name is carson!";
res="welcome to my blog!";
return SUCCESS;
}
public String reject() throws Exception
{
sorry="I'm sorry,this blog is secret!";
return ERROR;
}
}
上面的java文件中有兩個(gè)函數(shù),一個(gè)是excute,這是默認(rèn)的action,另一個(gè)是reject,是需要聲明的。
7.部署web項(xiàng)目
右鍵單擊項(xiàng)目文件夾,然后選擇Run As,再選擇Run alt="" />選擇上圖中的hello,則會(huì)呈現(xiàn)下圖所示的內(nèi)容:
選擇reject,則會(huì)出現(xiàn)下圖所示的內(nèi)容:
注:如果部署項(xiàng)目報(bào)錯(cuò),并出現(xiàn):HTTP Status 404 - There is no Action mapped for namespace [/] and action name [regist_Action] associated with context path [/wildcard1].相關(guān)字樣,可以通過如下方法解決:window->preferences->Java->Compiler->Building,然后將abort build when build path errors occur前的勾取消即可。
以上便是在Eclipse上開發(fā)web項(xiàng)目的整體流程。
相關(guān)案例查看更多
相關(guān)閱讀
- 云南網(wǎng)站建設(shè)公司
- 微信分銷
- 云南網(wǎng)站優(yōu)化公司
- 小程序用戶登錄
- 云南小程序代建
- 百度小程序開發(fā)公司
- 商標(biāo)注冊(cè)
- 正規(guī)網(wǎng)站建設(shè)公司
- 云南建站公司
- 云南小程序開發(fā)推薦
- 網(wǎng)站建設(shè)首選
- 支付寶小程序被騙
- 模版消息
- 二叉樹
- 云南小程序商城
- 汽車報(bào)廢系統(tǒng)
- 云南網(wǎng)站建設(shè)選
- 大理網(wǎng)站建設(shè)公司
- 商標(biāo)
- 網(wǎng)站建設(shè)特性
- 云南衛(wèi)視小程序
- 海報(bào)插件
- 報(bào)廢車
- 網(wǎng)站建設(shè)
- 云南網(wǎng)站建設(shè)公司哪家好
- 云南網(wǎng)站建設(shè)專業(yè)品牌
- 昆明小程序公司
- 定制小程序開發(fā)
- 云南網(wǎng)站建設(shè)哪家公司好
- 北京小程序開發(fā)