标签:开发 NPU 数据 context 跳转 use 价格 [1] contex
当发送一个请求时,除了使用RequestApi来接收参数之外,Struts2内部提供了3种接收参数的方式
接收参数的方式
在Action当中提供对应属性的set方法,会自动接收参数,并且会自动做类型转换
index.jsp
<form action="${pageContext.request.contextPath }/myform.action"
method="get">
用户名:<input type="text" placeholder="请输入用户名..." name="username"><br />
年龄:<input type="text" placeholder="请输入年龄..." name="age"><br />
爱 好:
<input type="checkbox" value="足球" name="hobby">足球
<input type="checkbox" value="篮球" name="hobby">篮球
<input type="checkbox" value="乒乓球" name="hobby">乒乓球<br />
<input type="submit" value="提交">
</form>
struts.xml
<struts>
<package name="struts" extends="struts-default" namespace="/">
<action name="myform" class="com.xzh.struts2.MyFormAction">
<!-- 结果页 -->
<result name="success">/myxq.jsp</result>
</action>
</package>
</struts>
MyFormAction.java
public class MyFormAction extends ActionSupport {
private String username;
private int age;
private List hobby;
public void setUsername(String username) {
this.username = username;
}
public void setAge(int age) {
this.age = age;
}
public void setHobby(List hobby) {
this.hobby = hobby;
}
public String execute() {
System.out.println(username);
System.out.println(age);
System.out.println(hobby);
return null;
}
}
缺点:接收少量数据可以,如果大量数据,还得要自己手动封装成对象
User.java
public class User {
private String username;
private Integer age;
private List hobby;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public List getHobby() {
return hobby;
}
public void setHobby(List hobby) {
this.hobby = hobby;
}
@Override
public String toString() {
return "User{" + "username='" + username + '\'' + ", age=" + age + ", hobby=" + hobby + '}';
}
}
index.jsp
<form action="${pageContext.request.contextPath }/myform.action"
method="get">
用户名:<input type="text" placeholder="请输入用户名..." name="user.username"><br />
年龄:<input type="text" placeholder="请输入年龄..." name="user.age"><br />
爱 好:
<input type="checkbox" value="足球" name="user.hobby">足球
<input type="checkbox" value="篮球" name="user.hobby">篮球
<input type="checkbox" value="乒乓球" name="user.hobby">乒乓球<br />
<input type="submit" value="提交">
</form>
struts.xml
<struts>
<package name="struts" extends="struts-default" namespace="/">
<action name="myform" class="com.xzh.struts2.myform.MyFormAction">
<!-- 结果页 -->
<result name="success">/myxq.jsp</result>
</action>
</package>
</struts>
MyFormAction.java
public class MyFormAction extends ActionSupport {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String execute() {
System.out.println(user);
return null;
}
}
User.java
public class User {
private String username;
private Integer age;
private List hobby;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public List getHobby() {
return hobby;
}
public void setHobby(List hobby) {
this.hobby = hobby;
}
@Override
public String toString() {
return "User{" + "username='" + username + '\'' + ", age=" + age + ", hobby=" + hobby + '}';
}
}
index.jsp
<form action="${pageContext.request.contextPath }/myform.action"
method="get">
用户名:<input type="text" placeholder="请输入用户名..." name="username"><br />
年龄:<input type="text" placeholder="请输入年龄..." name="age"><br />
爱 好:
<input type="checkbox" value="足球" name="hobby">足球
<input type="checkbox" value="篮球" name="hobby">篮球
<input type="checkbox" value="乒乓球" name="hobby">乒乓球<br />
<input type="submit" value="提交">
</form>
struts.xml
<struts>
<package name="struts" extends="struts-default" namespace="/">
<action name="myform" class="com.xzh.struts2.myform.MyFormAction">
<!-- 结果页 -->
<result name="success">/myxq.jsp</result>
</action>
</package>
</struts>
MyFormAction.java
public class MyFormAction extends ActionSupport implements ModelDriven<User>{
private User user = new User();
@Override
public User getModel() {
return user;
}
public String execute() {
System.out.println(user);
return null;
}
}
在开发时,可以配置一个input逻辑视图,错误时,可以在跳转到自己指定的页面, 在页面中可以显示错误信息
创建jsp页面
<form action="${pageContext.request.contextPath }/myform.action"
method="get">
商品名称:<input type="text" placeholder="请输入商品名称..." name="name"><br />
商品价格:<input type="text" placeholder="请输入商品价格..." name="price"><br />
<hr>
商品名称:<input type="text" placeholder="请输入商品名称..." name="name"><br />
商品价格:<input type="text" placeholder="请输入商品价格..." name="price"><br />
<hr>
商品名称:<input type="text" placeholder="请输入商品名称..." name="name"><br />
商品价格:<input type="text" placeholder="请输入商品价格..." name="price"><br />
<input type="submit" value="提交">
</form>
创建页面对象模型
public class Goods {
private String name;
private Double price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
@Override
public String toString() {
return "Goods{" + "name='" + name + '\'' + ", price=" + price + '}';
}
}
编写struts.xml
<struts>
<package name="struts" extends="struts-default" namespace="/">
<action name="myform" class="com.xzh.struts2.myform.MyFormAction">
<!-- 结果页 -->
<result name="success">/myxq.jsp</result>
</action>
</package>
</struts>
创建对应Action
public class MyFormAction extends ActionSupport {
private List<Goods> goodsList;
public List<Goods> getGoodsList() {
return goodsList;
}
public void setGoodsList(List<Goods> goodsList) {
this.goodsList = goodsList;
}
public String execute() {
System.out.println(goodsList);
return null;
}
}
在jsp当中添加表达式
<form action="${pageContext.request.contextPath }/myform.action"
method="get">
商品名称:<input type="text" placeholder="请输入商品名称..." name="goodsList[0].name"><br />
商品价格:<input type="text" placeholder="请输入商品价格..." name="goodsList[0].price"><br />
<hr>
商品名称:<input type="text" placeholder="请输入商品名称..." name="goodsList[1].name"><br />
商品价格:<input type="text" placeholder="请输入商品价格..." name="goodsList[1].price"><br />
<hr>
商品名称:<input type="text" placeholder="请输入商品名称..." name="goodsList[2].name"><br />
商品价格:<input type="text" placeholder="请输入商品价格..." name="goodsList[2].price"><br />
<input type="submit" value="提交">
</form>
基本上与List相同。
创建对应Action
public class MyFormAction extends ActionSupport {
private Map<String,Goods> goodsMap;
public Map<String, Goods> getGoodsMap() {
return goodsMap;
}
public void setGoodsMap(Map<String, Goods> goodsMap) {
this.goodsMap = goodsMap;
}
public String execute() {
System.out.println(goodsMap);
return null;
}
}
jsp
<form action="${pageContext.request.contextPath }/myform.action"
method="get">
商品名称:<input type="text" placeholder="请输入商品名称..." name="goodsMap['one'].name"><br />
商品价格:<input type="text" placeholder="请输入商品价格..." name="goodsMap['one'].price"><br />
<hr>
商品名称:<input type="text" placeholder="请输入商品名称..." name="goodsMap['two'].name"><br />
商品价格:<input type="text" placeholder="请输入商品价格..." name="goodsMap['two'].price"><br />
<input type="submit" value="提交">
</form>
key值为自己取的一个值
标签:开发 NPU 数据 context 跳转 use 价格 [1] contex
原文地址:https://www.cnblogs.com/xzh0717/p/10777385.html