Struts的簡單搭建(入門)css
過程摘要:(struts2下載:https://struts.apache.org/)html
(軟件要求:安裝好eclipse/myeclipse和tomcat)java
具體流程:web
(注意:若此時出現.jsp頁面找不到java build path問題,稍後將會解決)apache
將包導入新建工程:tomcat
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 }
打開dtd,將其內容複製至新建txt,再將txt重命名爲struts-2.3.dtd(以下圖)app
新建文件夾dtd,放置工程中。eclipse
打開struts-2.3.dtd,複製目標連接:jsp
打開菜單欄window中的preferences,如圖,添加struts-2.3.dtd測試
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>
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>
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版本。