Struts入门配置可以简单6步完成:
step1:从官网下载jar包
step2:新建dynamic web project项目,导入必要的jar包
step3:编写action类
step4:添加配置到struts.xml文件
step5:添加配置到web.xml文件
step6:定义jsp页面
下面详细每个步骤:
step1:从官网下载jar包
强调一点,如果需要更好的学习,官网、官方文档、API等是首选项,包括下载jar包也是,下面给出官网下载地址:
https://mirrors.tuna.tsinghua.edu.cn/apache/struts/2.5.13/struts-2.5.13-all.zip
下载后解压,可以找到lib目录中有很多jar包,对于入门学习并用不了这么多,在step2中会选几个必要的添加
step2:新建dynamic web project项目,导入必要的jar包
在eclipse中新建dynamic web project,项目名称为learnStruts2。导入Struts2必要的jar包,截图如下:
step3:编写action类
package control;
import com.opensymphony.xwork2.ActionSupport;
public class FirstStrutsAction extends ActionSupport{
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String execute(){
msg = "Hello world!!!";
System.out.println("Hello world!!!");
return SUCCESS;
}
public String myFun(){
msg = "my fun!!!";
System.out.println("my fun!!!");
return SUCCESS;
}
}
step4:添加配置到struts.xml文件
/helloWorld.jsp
/helloWorld.jsp
step5:添加配置到web.xml文件
learnStruts2
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
*.action
step6:定义jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
${msg}
至此框架已经配置好了,然后发布至tomcat或jboss,启动服务。在浏览器中访问,输出不同信息,地址如下:
http://localhost:8080/learnStruts2/helloWorld.action
http://localhost:8080/learnStruts2/myFun.action
最后项目中的文件目录截图:
参考文献:
struts官网API