struts2框架的简单搭建(入门)

Struts的简单搭建(入门)css


   过程摘要:(struts2下载:https://struts.apache.org/html

(软件要求:安装好eclipse/myeclipse和tomcat)java

  1.  导包:(apps里,blank案例是所需最简包)
  2.  Action类:(路径:src >  cn.itcast.hello(类所在包)  >  类名)
  3.    struts.xml:(路径:src >  struts.xml
  4.     web.xml:(核心过滤器的配置)
  5.     测试:

    

 


 

 具体流程:web

  • 建立一个新的project,选择动态web工程:

   

  

 (注意:若此时出现.jsp页面找不到java build path问题,稍后将会解决)apache

  • 导包:struts-2.3.24-all\struts-2.3.24\apps\struts2-blank\WEB-INF\lib目录下是项目所需包

    将包导入新建工程:tomcat

  • 建立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(以下图)app

新建文件夹dtd,放置工程中。eclipse

打开struts-2.3.dtd,复制目标连接:jsp

打开菜单栏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版本。

 

相关文章
相关标签/搜索