action与servlet API ModelDriven接口 异常

来源:互联网 发布:数据录入项目外包 编辑:程序博客网 时间:2024/05/17 01:08

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 stubreturn 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>


0 0
原创粉丝点击