Struts2 is an MVC framework developed from the WebWork framework.
sruts2 download address http://struts.apache.org/
1. Create a new web project as struts2, and modify the encoding format to utf-8
2. Import the jar package:
(Download address: http://struts.apache.org/download.cgi )
Copy all the jar packages under struts\apps\struts2-blank\WEB-INF\lib to the lib directory of the current web application.
Jar packages:
3. Configure struts2 in the web.xml file:
Copy the filter configuration in the struts\apps\struts2-blank\WEB-INF\web.xml file to the web.xml file of the current web application
4. Add the struts2 configuration file struts.xml under the classpath of the current web application:
Copy the struts.xml file under struts\apps\struts2-blank\WEB-INF\classes to the src directory of the current web application.
The above are the main steps of environment construction, for example
5. Create HelloWorldAction
public class HelloWorldAction{
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
//Action里面的方法返回值必须是字符串
public String execute() {
message="这是action的message";
return "success";
}
}
7. Write struts.xml
<struts>
<package name="default" namespace="/test" extends="struts-default">
<action name="HelloWorld" class="cn.fh.action.HelloWorldAction">
<result name="success">/HelloWorld.jsp</result>
<-- 这个"success"要和action类里的execute()的返回值相同-->
</action>
</package>
</struts>
8.HelloWorld.jsp
<body>
request:<%=request.getAttribute("name") %><br>
</body>