struts2下的helloworld(如何让第一个struts2跑起来)——struts2第一讲
生活随笔
收集整理的這篇文章主要介紹了
struts2下的helloworld(如何让第一个struts2跑起来)——struts2第一讲
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
注:文章中的全部圖片均在附件中明確表明??
首先要安裝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?
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?'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代碼?? <?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?
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代碼?? <?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文件 代碼如下:?
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 即可?
首先要安裝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?
Jsp代碼??
第五步: ?
修改WEB-INF下的web.xml文件?
代碼如下?
web.xml?
Xml代碼??
第六步: ?
新建action?
在src目錄下新建包com.test.action?
在包中新建一個action代碼如下?
LoginAction.java?
Java代碼??
第七步: ?
struts配置文件?
在src目錄下新建一個struts.xml代碼如下:?
Xml代碼??
注意,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文件 代碼如下:?
Jsp代碼??
至此,已經完成了代碼的書寫工作。接下去是發布;?
右鍵點擊struts2這個項目的名稱,在菜單中選擇myeclipse,在選擇add and remove project……即可,之后將出現圖三?
選擇project,點擊add發布到指定的tomcat即可、?
最后,打開瀏覽器。在瀏覽器 http://localhost:8080/struts2/login.jsp 即可?
總結
以上是生活随笔為你收集整理的struts2下的helloworld(如何让第一个struts2跑起来)——struts2第一讲的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于IOC的GUI框架设计与实现
- 下一篇: 使用Strust2框架写HelloWor