歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> struts2環境配置

struts2環境配置

日期:2017/3/1 10:19:24   编辑:Linux編程

Struts2框架,大多數框架都在使用。由於工作需要,開始做Java項目。先學個struts2。

一、下載struts2

有好多版本,我下載的是struts-2.2.1.1。

二、創建web項目,導入使用struts2所必須的jar包。

我使用的是MyEclipse 7.5。建立web項目,給項目添加外部引用包(project-properties-Java Build Path-Add External Jars...)。添加的包有:commons-fileupload-1.2.1.jar,commons-io-1.3.2.jar,commons-logging-api-1.1.jar,freemarker-2.3.16.jar,javassist-3.7.ga.jar,ognl-3.0.jar,struts2-core-2.2.1.1.jar,xwork-core-2.2.1.1.jar。注意:由於struts2版本的差異性,上面提到的包不一定滿足所有版本的需求。配置完struts2後,請部署運行一下。根據運行時的錯誤提示來添加jar包解決問題。比如,配置struts-2.2.1.1時需要commons-io-1.3.2.jar包和javassist-3.7.ga.jar包,但是2.1版本就不需要這兩個包。

三、編寫struts.xml文件

在MyEclipse項目中的src根目錄下建立一個struts.xml文件。(可以打開下載的struts2安裝包裡的apps目錄下的任意一個jar包,在裡面的WEB_INFR/src目錄下,尋找struts.xml文件,將該文件復制進項目的src根目錄下,將裡面的內容清空(只留下<struts>標簽和頭部標簽即可))

四、在web.xml中加入struts2 MVC框架啟動配置

和struts.xml文件的生成類似,在struts2安裝包裡找到web.xml文件,將裡面的<filter>和<filter-mapping>標簽及其內容拷貝進項目中的web.config文件即可。

五、struts2實例--簡單的登錄例子

5.1 編寫login.jsp頁面。代碼如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>Login</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

</head>

<body>

<s:form action="/login" method="post">
<s:label value="系統登陸"></s:label>
<s:textfield name="username" label="賬號" />
<s:password name="password" label="密碼" />
<s:submit value="登錄" />
</s:form>

</body>
</html>

Copyright © Linux教程網 All Rights Reserved