Struts的簡單搭建(入門) 過程摘要:(struts2下載:https://struts.apache.org/) (軟體要求:安裝好eclipse/myeclipse和tomcat) 具體流程: 創建一個新的project,選擇動態web工程: (註意:若此時出現.jsp頁面找不到java b ...
Struts的簡單搭建(入門)
過程摘要:(struts2下載:https://struts.apache.org/)
(軟體要求:安裝好eclipse/myeclipse和tomcat)
- 導包:(apps里,blank案例是所需最簡包)
- Action類:(路徑:src > cn.itcast.hello(類所在包) > 類名)
- struts.xml:(路徑:src > struts.xml)
- web.xml:(核心過濾器的配置)
- 測試:
具體流程:
- 創建一個新的project,選擇動態web工程:
(註意:若此時出現.jsp頁面找不到java build path問題,稍後將會解決)
- 導包:struts-2.3.24-all\struts-2.3.24\apps\struts2-blank\WEB-INF\lib目錄下是項目所需包
將包導入新建工程:
- 創建Action類:
- 編寫類HelloWorld:
1 package cn.itcast.helloworld; 2 3 public class HelloWorld { 4 5 public String hello(){ 6 7 System.out.println("hello world!"); 8 9 return "success!"; 10 } 11 12 }
- 創建struts.xml
- 添加文檔約束
打開dtd,將其內容複製至新建txt,再將txt重命名為struts-2.3.dtd(如下圖)
新建文件夾dtd,放置工程中。
打開struts-2.3.dtd,複製目標鏈接:
打開菜單欄window中的preferences,如圖,添加struts-2.3.dtd
- 編寫struts.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 <package name="hello" namespace="/hello" extends="struts-default"> 7 8 <action name="HelloWorld" class="cn.itcast.helloworld.HelloWorld" method="hello" > 9 <result name="success!">/index.jsp</result> 10 </action> 11 12 </package> 13 14 15 </struts>
- 編寫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" 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>struts2_day01</display-name> 4 5 <!-- struts2核心過濾器 --> 6 <filter> 7 <filter-name>struts2</filter-name> 8 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 9 </filter> <!-- ctrl+shift+t 可快速查type --> 10 <filter-mapping> 11 <filter-name>struts2</filter-name> 12 <url-pattern>/*</url-pattern> 13 </filter-mapping> 14 15 <welcome-file-list> 16 <welcome-file>index.html</welcome-file> 17 <welcome-file>index.htm</welcome-file> 18 <welcome-file>index.jsp</welcome-file> 19 <welcome-file>default.html</welcome-file> 20 <welcome-file>default.htm</welcome-file> 21 <welcome-file>default.jsp</welcome-file> 22 </welcome-file-list> 23 </web-app>
- 編寫 index.jsp(測試)
1 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'index.jsp' starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 </head> 22 23 <body> 24 Hello World! <br> 25 </body> 26 </html>
(註意:若此時出現.jsp頁面找不到java build path問題,則右擊工程名 > Built Path > configure build path)
添加自己安裝的tomcat版本。