Struts2 入门实例代码

阅读更多
Struts.xml 文件




    
    
    


HelloWorld.xml




	
    
        
             
	        
	            
	            
	        
          
		
		
			/WEB-INF/jsp/Logon.jsp				
                  		
		
					
			Main
			/WEB-INF/jsp/Logon.jsp		
		
		
			/WEB-INF/jsp/userList.jsp				crud!list
			/WEB-INF/jsp/userForm.jsp
		
		
			/WEB-INF/jsp/{1}.jsp
		         
    


LogonInterceptor.java

package com.jamesby.struts2;
import java.util.Map;
import com.jamesby.struts2.persist.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class LogonInterceptor extends AbstractInterceptor{
	@Override
	public String intercept(ActionInvocation ai) throws Exception {
		Map session = ai.getInvocationContext().getSession();
		User user = (User) session.get("user");
		if (null != user) {
			return ai.invoke();
		} else {
			return Action.LOGIN;
		}
	}
}


Logon.java

package com.jamesby.struts2;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.interceptor.validation.SkipValidation;
import com.jamesby.struts2.persist.DataBaseMock;
import com.jamesby.struts2.persist.User;
import com.opensymphony.xwork2.ActionSupport;
public class Logon extends ActionSupport implements SessionAware {
	private Map session;
	public void setSession(Map session) {
		this.session = session;
	}
	@SkipValidation
	public String doInput() throws Exception {
		return INPUT;
	}
	public String doLogon() throws Exception {
		User user = DataBaseMock.selectUserByUsernameAndPassword(getUsername(),
				getPassword());
		if (null == user)
			return INPUT;
		session.put("user", user);
		return SUCCESS;
	}
	private String username;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	private String password;
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}


UserAction.java

package com.jamesby.struts2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.struts2.interceptor.validation.SkipValidation;
import com.jamesby.struts2.persist.DataBaseMock;
import com.jamesby.struts2.persist.User;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction  extends ActionSupport {
	private User user=new User();
	private List userList;
	public List getSexArray() {
		List sexArray = new ArrayList();
		HashMap male = new HashMap();
		male.put("key",new Integer(1));
		male.put("value","男");
		HashMap female = new HashMap();
		female.put("key",new Integer(2));
		female.put("value","女");		
		sexArray.add(male);
		sexArray.add(female);
		return sexArray;
	}	
	public List getFromArray() {
		List fromArray = new ArrayList();
		HashMap bj = new HashMap();
		bj.put("id",new Integer(1));
		bj.put("name","北京");
		HashMap sh = new HashMap();
		sh.put("id",new Integer(2));
		sh.put("name","上海");		
		HashMap tj = new HashMap();
		tj.put("id",new Integer(3));
		tj.put("name","天津");
		fromArray.add(bj);
		fromArray.add(sh);
		fromArray.add(tj);
		return fromArray;
	}
	@SkipValidation
	public String doList() throws Exception {
		userList = DataBaseMock.selectAllUser();
		return "list";
	}
	@SkipValidation
	public String doInput() {
		return INPUT;
	}	
	public String doSave() {
		if (null == user.getUserid()) {
			user.setUserid(new Integer(DataBaseMock.getNewUserId()));
			DataBaseMock.saveUser(user);
		}else
		{
			DataBaseMock.updateUser(user);
		}
		return SUCCESS;
	}
	@SkipValidation
	public String doRead()
	{
		user = DataBaseMock.getUser(user.getUserid());
		return INPUT;
	}
	@SkipValidation
	public String doDelete() {
		DataBaseMock.deleteUser(user);
		return SUCCESS;
	}		
	public List getUserList() {
		return userList;
	}
	public void setUserList(List userList) {
		this.userList = userList;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
}


ExampleSupport.java

package com.jamesby.struts2;
import com.opensymphony.xwork2.ActionSupport;
public class ExampleSupport extends ActionSupport {
}


User.java

package com.jamesby.struts2.persist;
public class User {
	private Integer userid;
	private String username;
	private String password;
	private int sex;
	private int age;
	private int from;
	public int getFrom() {
		return from;
	}
	public void setFrom(int from) {
		this.from = from;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getSex() {
		return sex;
	}
	public void setSex(int sex) {
		this.sex = sex;
	}
	public Integer getUserid() {
		return userid;
	}
	public void setUserid(Integer userid) {
		this.userid = userid;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}


DataBaseMock.java

package com.jamesby.struts2.persist;
import java.util.ArrayList;
import java.util.List;
public class DataBaseMock {
	private static List _cache = new ArrayList();
	private static int userid = 0;
	static {
		User user = new User();
		user.setUserid(new Integer(0));
		user.setUsername("admin");
		user.setPassword("12345688");
		user.setFrom(1);// 北京
		user.setSex(1);// 男
		user.setAge(18);
		_cache.add(user);
	}
	public static User selectUserByUsernameAndPassword(String username,String password)
	{
		for (User u : _cache) {
			if (u.getUsername().equals(username)&&u.getPassword().equals(password)) {
				return u;
			}
		}
		return null;
	}
	public static List selectAllUser()
	{
		return _cache;
	}
	public static int getNewUserId() {
		return ++userid;
	}
	public static void saveUser(User user) {
		_cache.add(user);
	}
	public static void updateUser(User user) {
		deleteUser(user);
		saveUser(user);
	}
	public static void deleteUser(User user) {
		for (User u : _cache) {
			if (u.getUserid().intValue() == user.getUserid().intValue()) {
				_cache.remove(u);
				break;
			}
		}
	}
	public static User getUser(Integer userid) {
		return _cache.get(userid);
	}
}


Logon-validation.xml



    
        
			
        
    
    
        
			
        
    


UserAction-validation.xml



  
     
        
      
  
  
     
        
      
  
  
     
        
      
    
  
     
        
      
      
      	18
      	65
        
      
  
  
     
        
      
      


package.properties

requiredstring = $\{getText(fieldName)} is required.
password = Password
username = User Name
main.message = This is main page...................
label.userList= All User List

user.username=Username
user.password=Password
user.sex=Sex
user.age=Age
user.from=From

errors.required.username=Username is required
errors.required.password=Password is required
errors.required.sex=Sex is Required
errors.required.from=From is Required
errors.required.age.limit=Age between 18 and 65
errors.required.age.limit=Age between 18 and 65


Logon.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>


  Logon


 
  
  
  




userList.jsp

<%@ page contentType="text/html;charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags" %>


    <s:text name="label.userList"/>



">Add New User

evenodd"> evenodd">
用户ID 用户名 性别 年龄 来自 修改 删除
北京 上海 天津 ">修改 ">删除



userForm.jsp

<%@ page contentType="text/html;charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags" %>
User Form

    
    
    
    
    
    
    
    


应用程序目录结构

WEB-INF
│  web.xml
│
├─classes
│  │  commons-logging.properties
│  │  helloworld.xml
│  │  log4j.properties
│  │  struts.properties
│  │  struts.xml
│  │
│  └─com
│      └─jamesby
│          └─struts2
│              │  ExampleSupport.class
│              │  Logon-validation.xml
│              │  Logon.class
│              │  LogonInterceptor.class
│              │  package.properties
│              │  UserAction-validation.xml
│              │  UserAction.class
│              │
│              └─persist
│                      DataBaseMock.class
│                      User.class
│
├─jsp
│      Logon.jsp
│      Main.jsp
│      userForm.jsp
│      userList.jsp
│
├─lib
       commons-collections-3.1.jar
       commons-logging-1.0.4.jar
       freemarker-2.3.8.jar
       log4j-1.2.9.jar
       ognl-2.6.11.jar
       struts2-core-2.0.6.jar
       xwork-2.0.1.jar



  • jamesby-struts2-helloworld.rar (25 KB)
  • 描述: jamesby struts2 code
  • 下载次数: 5153

你可能感兴趣的:(Struts,JSP,freemarker,Apache,XML)