Struts2--属性设置方式

时间:2024-05-05 11:36:08

Struts2自动获取/设置数据的方式一共分为两种

  • 属性驱动(FieldDriven)
  • 模型驱动(ModelDriven)
  1. 属性驱动

属性又分为两种:

|- 基本数据类型

|- JavaBean属性类型

基本数据类型:实例

 <%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form action="hello" method="get">
姓名;<input type="text" name="name" size="10px" value="fuwh" readonly="readonly"/>&nbsp;
年龄:<input type="text" name="age" size="10px" value="23" readonly="readonly"/>
<button type="submit">提交</button>
</form>
</body>
</html>
package com.fuwh.struts;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven; public class HelloAction implements Action{ private String name;
private int age; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("执行了HelloAction的默认方法");
return SUCCESS;
} }
 <%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
欢迎${age }岁的${name }光临
</body>
</html>
 <?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="mypack" extends="struts-default">
<action name="hello" class="com.fuwh.struts.HelloAction">
<result name="success">hello.jsp</result>
</action>
</package>
</struts>

JavaBean属性类型

 package com.fuwh.struts;

 public class User implements java.io.Serializable{

     private static final long serialVersionUID = 1L;

     private String name;
private int age; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} }
 <%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form action="hello" method="get">
姓名;<input type="text" name="user.name" size="10px" value="fuwh" readonly="readonly"/>&nbsp;
年龄:<input type="text" name="user.age" size="10px" value="23" readonly="readonly"/>
<button type="submit">提交</button>
</form>
</body>
</html>
 package com.fuwh.struts;

 import com.opensymphony.xwork2.Action;

  public class HelloAction implements Action{ 

    private User user 

    public User getUser() {
return user;
}
public void setUser(User) {
this.user = user;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("执行了HelloAction的默认方法");
return SUCCESS;
} }
 <%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
欢迎${user.age }岁的${user.name }光临
</body>
</html>

struts.xml配置

 <?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="mypack" extends="struts-default">
<action name="hello" class="com.fuwh.struts.HelloAction">
<result name="success">hello.jsp</result>
</action>
</package>
</struts>

模型驱动

 <%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form action="hello" method="get">
姓名;<input type="text" name="name" size="10px" value="fuwh" readonly="readonly"/>&nbsp;
年龄:<input type="text" name="age" size="10px" value="23" readonly="readonly"/>
<button type="submit">提交</button>
</form>
</body>
</html>
 package com.fuwh.struts;

 import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven; public class HelloAction implements Action,ModelDriven{ private User user=new User(); @Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("执行了HelloAction的默认方法mae");
return SUCCESS;
}
@Override
public User getModel() {
// TODO Auto-generated method stub
System.out.println("执行了getModel方法");
return this.user;
}
}
 <%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
欢迎${age }岁的${name }光临
</body>
</html>
 <?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="mypack" extends="struts-default">
<action name="hello" class="com.fuwh.struts.HelloAction">
<result name="success">hello.jsp</result>
</action>
</package>
</struts>

Struts处理多值参数

普通类型

普通类型和负责类型一样,可以选择用集合来接收,也可以用数组来接收

复杂类型

package com.fuwh.demo;

public class User {

    private int age;
private String name; public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [age=" + age + ", name=" + name + "]";
} }
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form action="show" method="post">
兴趣爱好:<input type="text" name="stu[0].age" value="5">
<input type="text" name="stu[0].name" value="fuwh1"><br/>
<input type="text" name="stu[1].age" value="6">
<input type="text" name="stu[1].name" value="fuwh2"><br/>
<input type="text" name="stu[2].age" value="7">
<input type="text" name="stu[2].name" value="fuwh3"><br/>
<input type="submit">
</form>
</body>
</html>
package com.fuwh.demo;

import java.util.List;

import com.opensymphony.xwork2.Action;

public class Show implements Action{

    private List<User> stu;

    public List<User> getStu() {
System.out.println("execut the get method!");
return stu;
} public void setStu(List<User> stu) {
System.out.println("execute the set method!");
this.stu = stu;
} @Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("执行了action的默认方法");
System.out.println(stu);
return SUCCESS;
} }
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd"> <struts>
<!-- 开启debug模式,会自动加载配置文件等等,不用每次更改了配置文件就去重新启动下服务器 -->
<constant name="struts.devMode" value="true" /> <package name="test" extends="struts-default">
<action name="show" class="com.fuwh.demo.Show">
<result name="success">hello.jsp</result>
</action> </package> </struts>
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
学生们:${stu}
</body>
</html>

最后出力结果:

Struts2--属性设置方式

Struts2--属性设置方式