注:文章中的所有圖片均在附件中明確代表
首先要安裝jdk1.6以及tomcat6和myeclipse 對於這些配置的安裝 這裏再也不細細說明 由於網上好些地方都有的 給個連接吧 http://blog.sina.com.cn/s/blog_5116f6310100b889.html
其次是下載struts2
第一步:去struts21的官網 http://struts.apache.org/2.1.6/index.html
點擊下面圖1中的的download now,下載圖二中的全出便可。 下載後解壓待用;
第二步;
打開myeclipse tomcat的集成較簡單,很少講;新建一個web project 取名爲struts2;
第三步;
在剛剛下載的struts2-1-6目錄下的lib中複製出以下六個文件
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.6.jar
xwork-2.0.1.jar
以及(由於是struts2-1-6版本的。因此一下這個文件也必不可少)
commons-fileupload-1.2.1
而後粘貼到WebRoot/WEB-INF/lib便可;
第四步:
WebRoot目錄下新建一個login.jsp
代碼以下
login.jsp
css
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> java
<% web
String path = request.getContextPath(); apache
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 瀏覽器
%> tomcat
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> app
<html> eclipse
<head> jsp
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="login.action" method="post">
username: <input name="username" type="text"><br>
password: <input name="password" type="password"><br>
<input type="submit" value="submit">
</form>
</body>
</html>
第五步:
修改WEB-INF下的web.xml文件
代碼以下
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
<filter>
<filter-name>struts2</filter-name>
<!-- 控制器 -->
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<!-- 任何請求均有過濾器 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
第六步:
新建action
在src目錄下新建包com.test.action
在包中新建一個action代碼以下
LoginAction.java
package com.test.action;
public class LoginAction
{
//getter和setter方法 就是根據這裏的方法名來匹配客戶端的信息
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute() throws Exception
{
return "success";
}
//對應表單上的
private String username;
private String password;
}
第七步:
struts配置文件
在src目錄下新建一個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="struts2" extends="struts-default">
<action name="login" class="com.test.action.LoginAction">
<!-- result沒有名字是默認的success -->
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
注意,struts.xml中有句話是
!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
紅色這句可能會報錯,解決的方法是 將「http://」字樣去掉 其餘我不知道還有什麼方法,有高手知道請指點一二;
第八步;
新建result文件
在WebRoot目錄下新建一個result.jsp文件 代碼以下:
]
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'result.jsp' starting page</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
helloworld
username: ${requestScope.username }<br>
password: ${requestScope.password }
</body>
</html>
至此,已經完成了代碼的書寫工做。接下去是發佈;
右鍵點擊struts2這個項目的名稱,在菜單中選擇myeclipse,在選擇add and remove project……便可,以後將出現圖三
選擇project,點擊add發佈到指定的tomcat便可、
最後,打開瀏覽器。在瀏覽器 http://localhost:8080/struts2/login.jsp 便可
附件中有圖片和兩個源代碼;
struts2開發文件.rar (3.6 MB)
下載次數: 528
struts2發佈文件.rar (3.6 MB)
下載次數: 253