Development Process of the Struts2 application and the struts2 application process
Development Process of Struts2
To use Struts2 in eclipse for development, you need to import relevant jar packages as needed;
Download the related compressed packages from the official website. Here we download two files: struts-2.3.30-all.zipand struts-2.3.30-docs.zip, and decompress them to the same folder.
After a new Dymamic Web Project is created in eclipse, add Struts2 support for the Project:
1. Copy the Struts2 unzipping directory struts2-blank.war unzipping all the jar packages under the WEB-INF \ lib directory to the web project WebContent \ WEB-INF \ lib;
2. Import the copied jar packages to the project.
Start development with Struts2:
1. Define the interceptor in web. xml to intercept user requests:
If the Dynamic Web Project generated in eclipse does not automatically generate a web Project. xml file, you can find a default web from the Tomcat-related folder. xml file that copies this file to the project's WEB-INF directory (not under WEB-INF \ lib ).
You can also copy a web. xml file directly under the struts2-blank.war of the directory after the WEB-INF unzipping.
Modify the web. xml file as follows:
<? Xml version = "1.0" encoding = "UTF-8"?> <Web-app id = "WebApp_9" 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"> <! -- Define the core interceptor -->
<Filter> <filter-name> struts2 </filter-name> <filter-class> org. apache. struts2.dispatcher. ng. filter. strutsPrepareAndExecuteFilter </filter-class> </filter> <! -- Enable the core Interceptor to intercept all requests --> <filter-mapping> <filter-name> struts2 </filter-name> <url-pattern>/* </url-pattern> </filter-mapping> </web-app>
2. Define the JSP page containing form data:
Write a JSP page for request submission. The JSP page submits the request to the relevant Action class for processing.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
3. Define the Action class for processing user requests:
Write an Action class associated with the preceding JSP to process the request submitted by the JSP. Put it directly in the src directory in eclipse.
Package testAction; import com. opensymphony. xwork2.ActionSupport; public class Test_1Action extends ActionSupport {// define the member variable private String username that encapsulates request parameters; // setter and getter Methods public void setUsername (String username) {this. username = username;} public String getUsername () {return this. username;} // define the execute method public String execute () throws Exception {if (getUsername ()! = Null) {return SUCCESS;} else {return ERROR ;}}}
4. Compile the physical view resource file:
For simplicity, we put the physical view resource file directly under the webContent directory of the project.
Error. jsp:
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
Welcome. jsp:
<% @ Page language = "java" contentType = "text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <% @ taglib prefix =" s "uri ="/struts-tags "%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
5. Configure the Action class for processing user requests and configure the ing between the processing result of the Action and the physical view Resource ::
This allows JSP page requests to accurately find Action classes related to the JSP page.
To configure the Action, add struts under the src directory of the project. xml file, the default file for this file can also be found under the struts2-blank.war \ classes directory after the WEB-INF decompression, modify it to the following content:
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name = "struts. enable. dynamicMethodInvocation "value =" false "/> <constant name =" struts. devMode "value =" true "/> <package name =" test "extends =" struts-default "> <! -- Defines the Action named test_1. The Action class for processing the request is testAction. Test_1Action.java --> <action name = "test_1" class = "testAction. Test_1Action"> <! -- Define the ing between the logical view and the physical view --> <result name = "error"> error. jsp </result> <result name = "success"> welcome. jsp </result> </action> </package> </struts>