迈出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里会自行启动内置浏览器运行,很方便。

猜你喜欢

转载自to-zoe-yang.iteye.com/blog/1186088