Struts2开发步骤
1. 创建JavaWeb工程,将struts2的jar包拷贝到WebRoot/WEB-INF/lib目录
2. 修改web.xml配置文件,在文件添加struts2的核心过滤器
<filter>
<filter-name>s</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>s</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3. 编写struts2配置文件,文件创建在src 根目录,文件名字叫struts.xml
<?xml version="1.0" encoding="utf-8"?>
<struts>
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<constant name="struts.ui.theme" value="simple"></constant>
<package name="struts2" extends="struts-default">
<action name="HelloWorld" class="struts2.HelloWorld">
<result>/helloworld.jsp</result>
</action>
</package>
</struts>
4. 编写struts2的Action类
实体类和Action类的属性一定要遵守JavaBean规范
package struts;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
public static final String MESSAGE = "";
public String execute() throws Exception {
setMessage(MESSAGE);
return SUCCESS;
}
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
5. 编写jsp文件,导入struts2的标准标签库
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>helloworld</title>
</head>
<body>
<h1><s:property value="message" /></h1>
</body>
</html>
6. 运行并测试
Struts2的开发步骤
最新推荐文章于 2019-11-29 18:24:56 发布