第一个做成的struts(http://localhost:8088/StrutsTutorial/helloworld/HelloWorld.action)

1,我需要建立一个工程,名称为“StrutsTutorial”

2,引入lib。在webroot/WEB-INF/lib文件夹下放入六个jar文件:commons-fileupload-1.2.1.jar;commons-io-1.3.2.jar;freemarker-2.3.15.jar;ognl-2.7.3.jar;struts-core-2.1.8.1.jar;xwork-core-2.1.6.jar

3,配置web.xml文件。在WEB-INF文件夹下配置web.xml文件。



  StrutsTutorial

  
    struts2
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  

  
    struts2
    /*
  
    
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  


4,修改jsp文件。

首先,修改该index.jsp文件


	
		Hello World!
	
	
		

Hello World!


在webroot文件夹下新建一个文件夹pages

HelloWorld.jsp

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

	
		Struts 2 Hello World Application!
	
	
		

Current date and time is:

login.jsp

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

    
        Struts 2 Login Application!
    


	
		Login
	
	
		
	
	
	
	



loginsuccess.jsp

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

    
        Login Success
    
    
        

Login Successful

Hello !

5 在src文件夹下面新建一个struts.xml文件






    
    

	

		
			/pages/HelloWorld.jsp
		

		
	

	
		
			/pages/login.jsp
		

		
			/pages/login.jsp
			/pages/login.jsp
			/pages/loginsuccess.jsp
		
	


6 在src文件夹下新建三个package

分别包裹三个java文件

HelloWorldAction.java文件

package cn.hxex.struts.helloworld.action;

import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class HelloWorldAction extends ActionSupport {

	public static final String MESSAGE = "Struts 2 Hello World Tutorial!";

	public String execute() throws Exception {
		// 设置显示到页面的信息
		setMessage(MESSAGE);
		return SUCCESS;
	}

	private String message;

	public void setMessage(String message) {
		this.message = message;
	}

	public String getMessage() {
		return message;
	}

	public String getCurrentTime() {
		return new Date().toString();
	}

}


LoginAction.java

package cn.hxex.struts.login.action;

import cn.hxex.struts.login.bean.User;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class LoginAction extends ActionSupport {
	private User user;

	public String execute() throws Exception {
		boolean checkResult = "Admin".equals( user.getUsername() ) 
			&& "Admin".equals( user.getPassword() );
		if( checkResult ) {
			addActionMessage("Welcome you to here!");
			return SUCCESS;
		} else {
			addActionError("Invalid user name or password! Please try again!");
			return ERROR;
		}
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
}


User.java

package cn.hxex.struts.login.bean;

public class User {
	private String username;
	private String password;
	
	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;
	}
}



package cn.hxex.struts.login.bean;

public class User {
	private String username;
	private String password;
	
	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;
	}
}



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

    
        Struts 2 Login Application!
    


	
		Login
	
	
		
	
	
	
	




你可能感兴趣的:(struts)