strust2_002

阅读更多




	
	
		
		
			/success.jsp
		
		
		
			/index.jsp
		
		
		
			
		
		
		
			
		
		
		
			
		
		
			
		
		
			
		
		
	
    



package action;

import com.opensymphony.xwork2.ActionSupport;

import entity.User;

public class LoginAction extends ActionSupport {

	private User user;

	public String login() {
		String r = "success";
		if (null == user.getUserName() || "".equals(user.getUserName())) {
			addFieldError("error_name", "username is error!");
			r = "error";
		} else if (null == user.getPassword() || "".equals(user.getPassword())) {
			addFieldError("error_pwd", "password is error!");
			r = "error";
		}

		return r;

	}

	public User getUser() {
		return user;
	}

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

}



package action;

public class TestAction {
	public String test(){
		System.out.println("test");
		return null;
	}
}	



package action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class TestAction1 extends ActionSupport {

	private Map request;
	private Map session;
	private Map application;

	@SuppressWarnings("unchecked")
	public String test() {

		request = (Map) ActionContext.getContext().get(
				"request");
		session = ActionContext.getContext().getSession();
		application = ActionContext.getContext().getApplication();

		request.put("r1", "r1");
		session.put("s1", "s1");
		application.put("a1", "a1");

		return SUCCESS;
	}
}



package action;

import java.util.Map;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import sun.org.mozilla.javascript.internal.ContextAction;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware {
	
	private Map request;
	private Map session;
	private Map application;
	
	public String test(){
		
		request.put("r1", "r11");
		session.put("s1", "s11");
		application.put("a1", "a11");
		
		return SUCCESS;
	}
	
	
	public void setRequest(Map request) {
		this.request = request;
	}
	public void setSession(Map session) {
		this.session = session;
	}
	public void setApplication(Map application) {
		this.application = application;
	}
	
	
	
}





package action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction3 extends ActionSupport {

	private HttpServletRequest request;
	private HttpServletResponse response;
	private HttpSession session;
	private ServletContext application;

	public String test() {
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		session = request.getSession();
		application = ServletActionContext.getServletContext();

		request.setAttribute("r1", "r111");
		session.setAttribute("s1", "s111");
		application.setAttribute("a1", "a111");

		return SUCCESS;
	}
}



package action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction4 extends ActionSupport implements ServletRequestAware,
		ServletResponseAware, ServletContextAware {

	private HttpServletRequest request;
	private HttpServletResponse response;
	private HttpSession session;
	private ServletContext application;

	public String test() {

		session = request.getSession();

		request.setAttribute("r1", "r1111");
		session.setAttribute("s1", "s1111");
		application.setAttribute("a1", "a1111");

		return SUCCESS;

	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setServletContext(ServletContext application) {
		this.application = application;
	}

}



<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib  uri="/struts-tags" prefix="s" %>


<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'index.jsp' starting page
	
	
	    
	
	
	
  
  
  
    
username:
password:


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>



	
		

		My JSP 'success.jsp' starting page

		
		
		
		
		
		

	

	
		${user.userName }登录成功....
		
登录成功....
登录成功....



你可能感兴趣的:(struts2,servlet,API)