3.struts2 用action的属性接收参数

1.第一种

2.第二种 域模型(Domain Model)

  • 可以在action类中 private User user,User类包括属性name和age,此时url值应为 “http://localhost:8090/Struts2_0100_Introduction/user/hello/studentAdd?user.name=wusihan&user.age=8“,此时会先调用action对应类中的getUser方法再调用User类中setName方法,之后再次调用getUser方法后调用setAge方法将两个参数传入到action对应类中,与上面方法一致的是,struts2会找对应的getUser,setUser,setName,setAge方法,而不会去找action对应类中是否有名为user的属性或User类中是否有名为age,和名为name的属性

3.第三种 ModelDriven

  • action对应的java类中代码如下
package wusihanWeb;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven<User>{
    //此处与上面不同,必须人为new User因为这种方式不会调用setUser方法,会直接调用getModel方法来获取User对象,如果不在 此时new User那么此时user为空,调用该对象的setName或setAge时会空指针异常
    private User user = new User();
    @Override
    public User getModel() {
        System.out.println("getModel");
        return user;
    }
    public String execute(){
        return "success";
    }

}
  • url地址为:”http://localhost/wusihanWeb/user/hello?name=wusihan&age=9“.
  • struts2首先new出该action类,发现该action类实现了ModelDriven接口,此时调用action类的getModel方法获取此Model(User),然后调用User中的setName和setAge方法。
  • 这种方法不常用,但是包含一种特别重要的处理程序的思想:MVC
    Uesr就是模型,jsp文件就是试图,action类就是控制器

4.struts.xml中constant默认值

  • constant为
<constant name="struts.devMode" value="true" />
  • struts2-core-2.5.10.jar–org.apache.struts2–default.properties

5.简单的数据验证

  • 验证传入action中的参数是否满足验证,action类中代码如下
package wusihanWeb;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven<User>{

    private User user = new User();
    @Override
    public User getModel() {
        System.out.println("getModel");
        return user;
    }
    public String execute(){
        //根据不同的姓名内容返回不同的页面,从而达到验证的目的    
        if(user.getName()==null||!user.getName().equals("admin")){
            this.addFieldError("name", "name is error");
            return "error";
        }
        return "success";
    }

}
  • jsp中代码如下
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!--使用/struts-tags标签  -->
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>HelloStruts2</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>

  <body>
    Hello Struts2 <br>
    <!--s对应上面的prefix,fielderror对应action类中使用的方法addFieldError,fieldName对应addFieldError方法的第一个参数,theme为主题,simple为默认主题,下面这句话可以用简单的样式显示出addFieldError第二个参数的内容  -->
    <s:fielderror fieldName="name" theme="simple"/>
  </body>
</html>
发布了32 篇原创文章 · 获赞 0 · 访问量 956

猜你喜欢

转载自blog.csdn.net/hanzong110/article/details/55190822