您的位置:首页 > 编程语言 > Java开发

Struts2 接受参数方法

2012-12-06 14:45 183 查看
jsp:

<!-- 属性接受参数 -->

<form action="" name="f" method="post">

账号:<input type="text" name="username"/><br>

密码:<input type="password" name="password"/><br><br>

</form>

<input type="button" value="提交" onclick="javacript:document.f.action='<%=actionPath %>acceptparameter';document.f.submit()"/>

<hr>

<!-- PO接受参数 -->

<form action="" name="f1" method="post">

账号:<input type="text" name="user.username"/><br>

密码:<input type="password" name="user.password"/><br><br>

</form>

<input type="button" value="提交" onclick="javacript:document.f1.action='<%=actionPath %>acceptparameter1';document.f1.submit()"/>

<hr>

<!-- DTO接受参数 -->

<form action="" name="f2" method="post">

账号:<input type="text" name="userDTO.username"/><br>

密码:<input type="password" name="userDTO.password"/><br>

确认密码:<input type="password" name="userDTO.confirmingPassword"/><br>

</form>

<input type="button" value="提交" onclick="javacript:document.f2.action='<%=actionPath %>acceptparameter2';document.f2.submit()"/>

<hr>

<!-- ModelDriven接口接受参数 -->

<form action="" name="f3" method="post">

账号:<input type="text" name="user.username"/><br>

密码:<input type="password" name="user.password"/><br><br>

</form>

<input type="button" value="提交" onclick="javacript:document.f3.action='<%=actionPath %>acceptparameter3';document.f3.submit()"/>

action:

<!-- 属性接受参数 -->

public class Acceptparameter extends ActionSupport{
private String username;

private String password;

//get,set方法

@Override

public String execute() throws Exception {

System.out.println("username="+username+"password="+password);

return SUCCESS;

}

}

<!-- PO接受参数 -->

public class Acceptparameter1 extends ActionSupport{

private User user;

//get,set方法

@Override

public String execute() throws Exception {

System.out.println("username="+user.getUsername()+"password="+user.getPassword());

return SUCCESS;

}

}

PO:

public class User {

private String username;

private String password;

//get,set方法

}

<!-- DTO接受参数 -->

public class Acceptparameter2 extends ActionSupport{

private UserDTO userDTO;

private User user;

//get,set方法

@Override

public String execute() throws Exception {

System.out.println("DTOusername="+userDTO.getUsername()+"DTOpassword="+userDTO.getPassword() +"confirmingPassword="+userDTO.getConfirmingPassword());

User u=new User();

u.setUsername(userDTO.getUsername());

u.setPassword(userDTO.getPassword());

System.out.println("username="+u.getUsername()+"password="+u.getPassword());

return SUCCESS;

}

}

PO:

public class UserDTO {

private String username;

private String password;

private String confirmingPassword;

//get,set方法

}

<!-- ModelDriven接口接受参数 -->

public class Acceptparameter3 extends ActionSupport implements ModelDriven<User>{

private User user =new User();

//get,set方法

@Override

public String execute() throws Exception {

System.out.println("username="+user.getUsername()+"password="+user.getPassword());

return SUCCESS;

}

}

PO:

public class User {

private String username;

private String password;

//get,set方法

}

struts.xml:

<!-- 属性接受参数 -->

<action name="acceptparameter" class="Acceptparameter">

<result>

/Hello.jsp

</result>

</action>

<!-- PO接受参数 -->

<action name="acceptparameter1" class="Acceptparameter1">

<result>

/Hello.jsp

</result>

</action>

<!-- DTO接受参数 -->

<action name="acceptparameter2" class="Acceptparameter2">

<result>

/Hello.jsp

</result>

</action>

<!-- ModelDriven接口接受参数 -->

<action name="acceptparameter3" class="Acceptparameter3">

<result>

/Hello.jsp

</result>

</action>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: