(一),到struts官方网站下载struts-2.0.11.1-all.zip.URL:http://struts.apache.org/download.cgi#struts20111
(二),打开intelliJ IDEA新建空的Web Application
右键工程加入struts2的jar包:
commons-logging-1.0.4.jar,
freemarker-2.3.8.jar,
ognl-2.6.11.jar,
struts2-core-2.0.11.1.jar,
xwork-2.0.4.jar
(三),一切就绪,开始编码
首先配置web.xml,代码如下:
<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>
(四),在工程的src下新建struts.xml,struts.properties.这两个文件分别对应struts2-core-2.0.11.1.jar下的struts-default.xml和default.properties当然你可以到struts2-core-2.0.11.1.jar下拷贝这两个文件并做适当的修改。struts.properties这里不做任何修改
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>
<include file="struts-default.xml"/>
<package name="default" extends="struts-default" namespace="/user">
<action name="List" class="user.actions.UserAction" method="listUser">
<result name="success">/user/listUser.jsp</result>
</action>
<action name="Edit" class="user.actions.UserAction" method="simpleUser">
<result name="success">/user/editUser.jsp</result>
</action>
<action name="Delete" class="user.actions.UserAction" method="removeUser">
<result name="success" type="redirect">List.action</result>
</action>
<action name="Store" class="user.actions.UserAction" method="storeUser">
<result name="success" type="redirect">List.action</result>
</action>
</package>
</struts>
(五),新建Action代码如下:
package user.actions;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Collection;
import user.model.User;
import user.dao.UserDao;
public class UserAction extends ActionSupport {
private String userID;
private String[] userIDs;
private User user;
private Collection<User> users;
private UserDao userDao = UserDao.getInstance();
public UserAction(){}
public String execute() throws Exception {
return super.execute();
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String[] getUserIDs() {
return userIDs;
}
public void setUserIDs(String[] userIDs) {
this.userIDs = userIDs;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Collection<User> getUsers() {
return users;
}
public void setUsers(Collection<User> users) {
this.users = users;
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
//==============================
//all users
public String listUser(){
users = userDao.getUsers();
return SUCCESS;
}
//simple user
public String simpleUser(){
user = userDao.getUser(userID);
return SUCCESS;
}
//store user
public String storeUser(){
userDao.storeUser(user);
return SUCCESS;
}
//delete users
public String removeUser(){
if(userID != null)
userDao.deleteUser(userID);
else
userDao.removeUsers(userIDs);
return SUCCESS;
}
}
(六),Dao这里简单起见并没有连接数据库,而是用ConcurrentHashMap作为数据源
package user.dao;
import user.model.User;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Collection;
/**
* Created by IntelliJ IDEA.
* User: ibm
* Date: 2008-5-27
* Time: 14:23:17
* To change this template use File | Settings | File Templates.
*/
public class UserDao {
private static ConcurrentMap<String, User> dataMap;
private static UserDao userDao = new UserDao();
static{
dataMap = new ConcurrentHashMap<String,User>();
dataMap.put("1000",new User("1000","jim","123","beijing"));
dataMap.put("1001",new User("1001","steven","234","shanghai"));
dataMap.put("1002",new User("1002","oscar","123","shanghai"));
}
public UserDao(){}
public static UserDao getInstance(){
return userDao;
}
public User getUser(String userID){
return dataMap.get(userID);
}
public Collection<User> getUsers(){
return dataMap.values();
}
public void removeUsers(String[] userid){
for(String uid:userid){
dataMap.remove(uid);
}
}
public void deleteUser(String userID){
dataMap.remove(userID);
}
public void storeUser(User user){
dataMap.put(user.getUserID(),user);
}
}
(七),Model数据Bean
package user.model;
/**
* Created by IntelliJ IDEA.
* User: ibm
* Date: 2008-5-27
* Time: 14:02:23
* To change this template use File | Settings | File Templates.
*/
public class User {
private String userID;
private String userName;
private String passWord;
private String address;
public User(){}
public User(String userID,String userName,String passWord,String address){
this.userID = userID;
this.passWord = passWord;
this.userName = userName;
this.address = address;
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
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;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
(八),页面显示listUser.jsp
<s:form id="formSubmit" action="Delete" theme="simple">
<s:iterator value="users">
<tr>
<td>
<input type="checkbox" name="userIDs" value="<s:property value='userID'/>"/>
</td>
<td><s:property value="userID"/></td>
<td><s:property value="userName"/></td>
<td><s:property value="passWord"/></td>
<td><s:property value="address"/></td>
<td>
<s:url id="edit" action="Edit" namespace="/user"><s:param name="userID" value="userID"/></s:url><a href="${edit}">EDIT</a>
</td>
<td>
<s:url id="delete" action="Delete" namespace="/user">
<s:param name="userID" value="userID"/></s:url><a href="${delete}">DELETE</a>
</td>
</tr>
</s:iterator>
<s:a href="javaScript:sunmit();">Delete!</s:a>
<s:a href="editUser.jsp">Add user!</s:a>
</s:form>
(九),编辑添加页面editUser.jsp
<s:if test="null == user">
ADD USER!
</s:if>
<s:else>
EDIT USER!
</s:else>
<s:form action="Store" name="/user">
<s:textfield name="user.userID" label="USERID:"/>
<s:textfield name="user.userName" label="USERNAME:"/>
<s:textfield name="user.passWord" label="PASSWORD:"/>
<s:textfield name="user.address" label="ADDRESS:"/>
<s:submit value="submit"/>
</s:form>
到此一个简单的入门struts2应用就算完成了。由于很简单这里并没有加入struts2的验证框架。