To put it bluntly, this simple struts has been tossing around for a long time, and it took about three days to solve it. Below I will summarize the development steps: (I am using myeclipse );
1. Create an exercise3 WEB Project
2. Configure the Web. xml file
Add the following code:
<filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping>
3. Add a class library
Place the jar package for example under WEB-INF/lib
4. Create a class
The Code is as follows:
Package CN. WWH. www. struts;/*** function: ** @ author @ * @ version 1.0 * @ Creation Time: 08:44:26 */public class firstaction {Public String <span style = "color: # ff0000;"> firtstmethod </span> () {system. out. println ("Hello word, this my first Struts! "); Return" hello ";}}
5. Create a New View/Hello/welcome. jsp file under webroot.
The Code of welcome. jsp is as follows:
<pre name="code" class="html"><%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
6. Create a struts. xml file under SRC
The Code is as follows:
<?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><package name="default" namespace="/<span style="color:#3333ff;">test</span>" extends="struts-default"><action name="<span style="color:#3333ff;">hello</span>" class="cn.wwh.www.struts.FirstAction"method="<span style="color:#ff0000;">firtstMethod</span>"><result name="hello" type="redirect"><param name="location">/views/hello/welcome.jsp</param></result></action></package></struts>
7. deploy it in Tomcat
Enter http: // localhost: 8080/exercise3/test/Hello. Action in the browser.
The address bar is changed because the Redirect (redirection) method is used in the configuration file.
Result chart:
Note ):
1. The jar package cannot be imported repeatedly. Otherwise, a jar conflict occurs. Due to various jar packages, this exception occurs when Tomcat is started.
2. The access address entered in the browser is incorrect. This address combination is related to the namespace and action name in the Struts. xml file.
General access method:
Http: // ip: Port/contextpath/namespace/actionname [. Action]