1、下载 struts-2.0.14-all.zip 下载地址就不多少了 自己到官方网站去下;
2、新建web工程 名叫 struts2 导入如下jar包
3、修改 web.xml 文件 如下 :
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
4、新建个登陆页面 long.jsp 里面内容为:
<form action="login.action" method="post">
username:<input type="text" name="usename" /><br>
password:<input type="password" name="password" /><br>
<input type="submit" name="submit" value="提交">
</form>
5、新建个java类:
package jz.crc.com;
public class longAction {
private String usename ;
private String password;
public String getUsename() {
return usename;
}
public void setUsename(String usename) {
this.usename = usename;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute() throws Exception {
return "success";
}
}
6、在src目录下新建struts.xml文件 ,内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default">
<action name="login" class="jz.crc.com.longAction"> <!-- login 对应form中的action class 表示出来请求的方法-->
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
7、新建 result.jsp 用于接收 返回数据:
username:${requestScope.usename }<br>
password:${requestScope.password }
8、将工程添加到tomcat中 运行 这样第一个struts 工程就完成了
第一次接触struts2 有朋友想学struts2的可以一起交流 ,一起学习。