Simple construction of Struts (Introduction)
Process summary: (struts 2 download: https://struts.apache.org/)
(software requirements: install eclipse/myeclipse and tomcat)
- Guide Package: (in apps, blank case is the simplest package needed)
- Action class: (path: SRC > CN. Itcast. Hello (the package of the class) > class name)
- struts.xml: (path: SRC > struts. XML)
- web.xml: (configuration of core filter)
- Test:
Specific process:
- Create a new project and select dynamic web project:
(Note: if the. jsp page cannot find the java build path problem, it will be solved later.)
- Guide Package: the package required by the project is in the directory of struts-2.3.24-all \ struts-2.3.24 \ apps \ struts 2-blank \ WEB-INF \ Lib
To import a package into a new project:
- Create Action class:
- Write HelloWorld class:
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 }
- Create struts.xml
- Add document constraint
Open DTD, copy its contents to new TXT, and rename txt to struts-2.3.dtd (as shown below)
Create a new folder dtd and place it in the project.
Open struts-2.3.dtd and copy the target link:
Open preferences in the menu bar window, as shown in the figure, and add struts-2.3.dtd
- Write 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>
- Write web.xml (configure core filter)
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 Core filter --> 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 Quick check 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>
- Write index.jsp (test)
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>
(Note: if the. jsp page cannot find the java build path problem, right-click the project name > build path > configure build path)
Add your own version of tomcat.