action与servlet API ModelDriven接口 异常

本文介绍如何在Struts2框架中访问不同的作用域,包括通过ActionContext类、特定接口及ServletActionContext访问请求、会话和应用作用域。同时展示了ModelDriven接口的应用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.通过ActionContext类访问

ActionContext context=ActionContext.getContext();
		
		context.put("name", "request:tom");
		context.getApplication().put("name", "application:tom");
		context.getSession().put("name", "session:tom");


${applicationScope.name}<br/>
		${sessionScope.name}<br/>
		${requestScope.name}<br/>



2.通过特定接口


ServletRequestAware
ServletResponseAware
ServletContextAware

3.通过ServletActionContext访问




ModelDriven接口

public class LoginAction extends ActionSupport implements ModelDriven<UserModel>{
	private static final long serialVersionUID = 1L;
	//创建UserModel实例
    private UserModel user=new UserModel();
    //getter方法,必须实现
	public UserModel getModel() {
		// TODO Auto-generated method stub
		return user;
	}
	//重载execute方法
    public String execute() throws Exception {
    	//得到ActionContext实例
    	ActionContext context=ActionContext.getContext();
    	//将(“user”,user)放入ActionContext中
    	context.put("user", user);
    	return SUCCESS;
    }
}

public class UserModel {
	//UserModel类的name、age、address、telephone属性
	private String name;
	private String age;
	private String address;
	private String telephone;
	//各个getter、setter方法
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getTelephone() {
		return telephone;
	}
	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

}


<s:form action="user">
			<s:textfield label="Name" name="name"></s:textfield>
			<s:textfield label="Age" name="age"></s:textfield>
			<s:textfield label="Telephone" name="telephone"></s:textfield>
			<s:textfield label="Address" name="address"></s:textfield>
			<s:submit></s:submit>
		</s:form>


<center>
		<s:property value="#user.name"/><br/>
		<s:property value="#user.age"/><br/>
		<s:property value="#user.telephone"/><br/>
		<s:property value="#user.address"/><br/>
	</center>



异常

<struts>
    
   
    <package name="default" namespace="/" extends="struts-default">
        <global-results>
                       <result name="Exception">/Exception.jsp</result>
                       <result name="SQLException">/SQLException.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.sql.SQLException" result="SQLException"/>
            <exception-mapping exception="java.lang.Exception" result="Exception"/>
        </global-exception-mappings>
      
        <action name="user" class="com.action.UserAction">
            <exception-mapping exception="com.action.SecurityException" result="login"/>
        
		<result name="login" >/loginException.jsp </result>
            <span style="white-space:pre">	</span><result>/success.jsp</result>
        </action>
    
    </package>
    
</struts>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值