邁出Struts2的第一步


       開始實習啦!主要從事Web應用開發!目前就是ERP項目的重構~壓力很大啊!雖然以前做的項目都是用Java寫的,但是Web方面沒有怎麼接觸,也就對Servlet有點了解~沒辦法啦!Struts2必須得會啊~

      一步一步慢慢來吧!開始我的Struts之旅了!

      萬物都是HelloWorld!

      先構建好項目的框架吧!至少得跑起來!

      IED:Eclipse Java EE IDE for Web Developers. Version: Helios Service Release 2

      WebServer:Tomcat6.0

      不得不感慨Eclipse真的很方便啊!

 

第一步:創建Web項目,項目名:HelloWorld



 

完成這一步後,項目的雛形就出來了!



 

 

第二步:導入相關的lib。主要就是Struts2的lib啦~我是直接從下載的Struts2裏自帶的程序裏copy過來的。

 

第三步:配置Web.xml。

           還是從程序裏拷貝過來,然後刪除一些,留下我們需要的。

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Struts Blank</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.html</welcome-file>
    </welcome-file-list>

</web-app>

 

 

第四部: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" extends="struts-default">
        <action name="hello">
            <result >
                helloworld.jsp
            </result>
        </action>
    </package>

</struts>

 

 

第五步:

視圖index.html和helloworld.jsp的創建。

 

index.html

 

<!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>
	This is index.html!
</body>
</html>

 

helloworld.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	This is Hello.jsp 
</body>
</html>

 

 

最終的程序的結構爲:

 



 

 

 

創建Server運行。

 

 

選擇後,一步一步往下,Eclipse裏會自行啓動內置瀏覽器運行,很方便。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章