码迷,mamicode.com
首页 > 其他好文 > 详细

Struts2 基础总结

时间:2015-08-10 22:24:44      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:struts2.0


1 Struts2中获取Action的方式:

    可以通过继承ActionSupport的方式获取;

2 Struts2中常用 的传递参数方式:

    a:可以通过属性传递参数,表单的属性名称和javabean的属性名称相对应,在Action添加对应的属性,并设置get和set方法即可;

    b:可以通过javabean传递参数,Action中设置javabean类型的属性,添加get和set方法,表单中使用,对象.属性的形式作为属性名称;

3 struts2访问servletAPI:

    访问假的API(用Map模拟的ServletAPI):

     实例化ActionContext,由此获取;

package com.st.action;

import java.util.HashMap;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.st.user.User;

public class LoginAction extends ActionSupport {
	private Map<String, Object> request=new HashMap<String, Object>();
	private Map<String, Object> session=new HashMap<String, Object>();
	private Map<String, Object> application=new HashMap<String, Object>();
	private User user;	
    public User getUser() {
		return user;
	}

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


	public String checkLogin(){
		ActionContext actionContext=ActionContext.getContext();
		request=(Map<String, Object>) actionContext.get("request");
		session=actionContext.getSession();
		application=actionContext.getApplication();
		
		request.put("r", "request中的值");
		session.put("s", "session中的值");
		application.put("a", "application中的值");
		return "suc";
	}
}


        访问真的ServletAPI:

        用到ServletActionContext,来获取request;

package com.st.action;

import java.util.HashMap;
import java.util.Map;

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

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.st.user.User;

public class LoginAction extends ActionSupport {
	HttpServletRequest request=null;
	HttpSession session=null;
	ServletContext application=null;
	private User user;	
    public User getUser() {
		return user;
	}

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


	public String checkLogin(){		
		request=ServletActionContext.getRequest();
		session=request.getSession();
		application=session.getServletContext();
		
		request.setAttribute("r", "request中的值");
		session.setAttribute("s", "session中的值");
		application.setAttribute("a", "application中的值");
		return "suc";
	}
}

 4 struts2中的拦截器:

     继承于AbstractInterceptor,在struts.xml声明拦截器,在Action配置拦截器;


package com.st.in;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginInterceptor extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		System.out.println("in Interceptor");
		String result=invocation.invoke();
		System.out.println("out Interceptor");
		return result;
	}

}

package com.st.in;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class TwoIntercepto extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		System.out.println("in 2 Interceptor");
		String res=invocation.invoke();
		System.out.println("out 2 Interceptor");
		return res;
	}

}

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>	
	<package name="user" namespace="/user" extends="struts-default"> 

		<interceptors>
			<interceptor name="loginIn" class="com.st.in.LoginInterceptor"></interceptor>
			<interceptor name="twoIn" class="com.st.in.TwoIntercepto"></interceptor>
			<interceptor-stack name="allIn">
				<interceptor-ref name="defaultStack"></interceptor-ref>
				<interceptor-ref name="loginIn"></interceptor-ref>
				<interceptor-ref name="twoIn"></interceptor-ref>
			</interceptor-stack>
		</interceptors>
        <action name="login" class="com.st.action.LoginAction" method="checkLogin">            	
        	<interceptor-ref name="allIn"></interceptor-ref>
        	<result name="suc">/success.jsp</result>         	       	
        </action>        
   </package>   
</struts>

说明 invocation.invoke();是执行下一个资源,进入下一个拦截器或者Action等;如果没有执行invocation.invoke(),将根据return的字符串在struts.xml匹配result。



拦截用户名 aaa,若出现,跳转到haha.jsp.没有出现最终执行action;

package com.st.in;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginInterceptor extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		
		Map<String, Object> request=(Map<String, Object>) ActionContext.getContext().get("request");
		String uname=(String) request.get("user.uname");
		System.out.println(uname);
		if(uname.equals("aaa")){
        	return "haha";
        }	
		String result=invocation.invoke();
        	
		return result;
	}

}


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>	
	<package name="user" namespace="/user" extends="struts-default"> 

		<interceptors>
			<interceptor name="loginIn" class="com.st.in.LoginInterceptor"></interceptor>
			<interceptor name="twoIn" class="com.st.in.TwoIntercepto"></interceptor>
			<interceptor-stack name="allIn">
				<interceptor-ref name="defaultStack"></interceptor-ref>
				<interceptor-ref name="loginIn"></interceptor-ref>
				<interceptor-ref name="twoIn"></interceptor-ref>
			</interceptor-stack>
		</interceptors>
        <action name="login" class="com.st.action.LoginAction" method="checkLogin">            	
        	<interceptor-ref name="allIn"></interceptor-ref>
        	<result name="suc">/success.jsp</result>         	       	
        	<result name="haha">/haha.jsp</result>         	       	
        </action>        
   </package>   
</struts>

5 struts2的路径问题:

   当struts2返回页面的路径含有namespace的时候,在该页面跳转到其他jsp页面会产生报错,原因是此时连接含有namespace,此时可以使用跟路径解决此问题。

   在返回的jsp页面加上:

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

<base href="<%=basePath %>">

此时页面跳转默认是在basePath基础上的,可以避免命名空间带来的问题;

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 <%
 	String path=request.getContextPath();
    String basePath=request.getScheme()+"://"+request.getServerName()+":"
 	+request.getServerPort()+path+"/";
 %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath %>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${request.r}<br>
${session.s}<br>
${application.a}<br>
<a href="haha.jsp">haha</a>
</body>
</html>

当前页面是由,action返回的,在<html>之上定义类basePath,在head引入了basePath,当前页面所有路径都是建立在此基础上的可以避免错误。












Struts2 基础总结

标签:struts2.0

原文地址:http://blog.csdn.net/liangwenmail/article/details/47403173

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!