Home > Java > javaTutorial > A simple case of configuring java Struts2

A simple case of configuring java Struts2

Y2J
Release: 2017-05-03 14:24:37
Original
1158 people have browsed it

This article mainly introduces the configuration and simple cases of java Struts2. Friends who need it can refer to the configuration and simple cases of

Struts2:

1. Create a dynamic web project (let it automatically generate the web.xml file when creating)

2. Introduce relevant jar packages

3. Configure in web.xml

(The first file loaded after starting the tomcat server is web.xml)

Add in the configuration Filter:

<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>
Copy after login

4. Create the core file of struts (struts.xml) and create it in the Java Resources-src file directory with the 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>
</struts>
Copy after login

5. Also create an Action class in the Java Resources-src file directory, inherit from ActionSupport, and override the execute method in the parent class:

public class HelloWorldAction extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("执行Action");
return SUCCESS;
}
}
Copy after login

6. In struts.xml, < Add to the struts> tag:

<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="helloworld" class="default package.HelloWorldAction">
<result>/result.jsp</result>
</action>
</package>
</struts>
Copy after login

7. Create a view (create result.jsp in the WebRoot directory):

<body>
This is result.jsp!
</body>
Copy after login

8. Debugging Run

The above is the detailed content of A simple case of configuring java Struts2. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template