1,先下载struts2的包,google一下
2,建一个动态WEB工程,本例叫test
3,导入最低限度的所需的包,从struts2自带的struts2-blank例子里找(struts-2.2.1.1\apps\struts2-blank\WEB-INF\lib),导入后就是下面这个样子
4,编辑web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>test</display-name> <!-- 配置好下面的filter,基本上struts2就可以用了 --> <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> </web-app>
5, 写action
package com.ahuzl;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.ScopedModelDriven;
public class HelloWorld extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = -63925948007130371L;
//struts1中需要写在actionform中的属性及get,set方法
//现在直接写在action中就可以了
//当然也可以把属性及get,set方法单独写成一个POJO类,后面说
private String password;
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
//一个与struts1的区别,struts2不用必须重写execute方法了,
//只要是返回值是String的方法就可以被action执行
//具体执行那个,在struts.xml里配置
public String index() {
System.out.println("---index----");
return SUCCESS;
}
public String transfer() {
System.out.println("---transfer----");
System.out.println("---username: " + username);
System.out.println("---password: " + password);
return SUCCESS;
}
}
6,写个JSP作为前端(简单的用户输入)