唉我的第一篇日志真難發布,內容剛剛寫完,不小心點了一個X火狐關了就沒了!
一、在下載的struts2.1.6-all.zip找到以下包放入項目中WEB-INF/lib/下;
1.commons-fileupload-1.2.1.jar <--文件上傳
2.commons-logging-1.0.4.jar <--日志
3.freemarker-2.3.13.jar <--模板引擎
4.ognl-2.6.11.jar <--Object-Graph Navigation Language
5.struts2-core-2.1.6.jar <--struts2核心
6.xwork-2.1.2.jar <--xwork核心
二、在項目的web.xml文件中添加配置信息
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
3 <display-name>Struts2HelloWord</display-name>
4
5 <!--過濾器 -->
6 <filter>
7
8 <!-- 指定這個過濾的名字 -->
9 <filter-name>struts2</filter-name>
10
11 <!-- 指定這個過濾器的類路徑. 這個類是'struts2-core-2.1.6.jar'核心過濾分配器 -->
12 <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
13 </filter>
14
15 <!--過濾器映射 -->
16 <filter-mapping>
17
18 <!-- 指定當前所有請求至struts2過濾器 -->
19 <filter-name>struts2</filter-name>
20 <!-- 通配符*代表所有 -->
21 <url-pattern>/*</url-pattern>
22 </filter-mapping>
23
24 <!-- 指定訪問當前項目的主頁 -->
25 <welcome-file-list>
26 <welcome-file>index.jsp</welcome-file>
27 </welcome-file-list>
28 </web-app>
三、新建一個名為HelloAction的Action類(此類在helloworld包中)
1 package helloworld;
2
3 public class HelloAction {
4
5 //用于保存用戶名
6 private String userName;
7 //用于設置給welcome.jsp的信息
8 private String massage;
9
10 //setter getter方法
11 public void setUserName(String userName){
12 this.userName = userName;
13 }
14
15 public String getUserName(){
16 return userName;
17 }
18
19 public void setMassage(String massage){
20 this.massage = massage;
21 }
22
23 public String getMassage(){
24 return massage;
25 }
26
27 //默認處理用戶請求的方法
28 public String execute() throws Exception{
29
30 //判斷用戶名不能為空白
31 if(!getUserName().equals("")){
32 setMassage("scorpion剌客:"+getUserName()+"歡迎來到struts2的世界!");
33 //在控制臺輸出massage成員
34 System.out.println(getMassage());
35
36 //返回success字符串
37 return "success";
38 }else{
39
40 //返回error字符串
41 return "error";
42 }
43 }
44 }
四、在src中新建一個struts.xml文件(如果你用文本編輯器struts.xml應在項目/WEB-INF/classes下)
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <!--定義當前xml文檔為struts2配置文檔 -->
4 <!DOCTYPE struts PUBLIC
5 "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
6 "http://struts.apache.org/dtds/struts-2.1.dtd">
7 <!-- 開始配置struts2 -->
8 <struts>
9 <!-- 定義一個helloworld包,在此包的所有類都繼承struts-default包(此包在struts2-core-2.1.6.jar/struts-default.xml里)-->
10 <package name="helloworld" extends="struts-default">
11 <!-- 定義一個名為hello的Action 指定類文件為helloworld.HelloAction -->
12 <action name="hello" class="helloworld.HelloAction">
13 <!-- 當helloworld.HelloAction類的execute方法返回success字符串給struts2框架時,執行welcome.jsp -->
14 <!-- 以下默認為<result name="success">……-->
15 <result>/welcome.jsp</result>
16 <!-- 當helloworld.HelloAction類的execute方法返回error字符串給struts2框架時,執行error.jsp -->
17 <result name="error">/error.jsp</result>
18 </action>
19 </package>
20 </struts>
五、新建一個index.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
2 <%--struts2標簽庫配置--%>
3 <%@ taglib prefix="s" uri="/struts-tags" %>
4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5 <html>
6 <head>
7 <title>歡迎</title>
8 </head>
9 <body>
10 <%--向客戶端生成一個form表單 --%>
11 <s:form action="hello">
12 <%-- 生成一個text文本--%>
13 <s:textfield name="userName" label="請問你的名字是"/>
14 <%-- 生成一個提交按鈕--%>
15 <s:submit value="告訴他"/>
16 </s:form>
17 </body>
18 </html>
六、新建一個welcome.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <title>welcome</title>
7 </head>
8 <body>
9
10 ${requestScope.massage}
11 </body>
12 </html>
七、新建一個error.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title></title>
6 </head>
7 <body>
8 您不可能沒有名字!(偷偷告訴我可以啦!^_^)
9 </body>
10 </html>
八、編譯 部署 訪問
這是我在http://www.tkk7.com/ 上第一篇日志希望能夠認識更多的朋友!