开门见山
直接上代码
Struts 2 中的数据填充有3中方式:
1、字段填充:
public class TestActionSupport extends ActionSupport {
此处若为Private motifi
/*
private String userId;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
*/
public String userId;
@Override
public String execute() throws Exception {
System.out.println(userId);
return super.execute();
}
}
JSP页面
<!--JSP页面写法-->
<form action="/struts2-class/user/testActionSupport">
userId:<input type="text" name="userId"/><br/>
<input type="submit" />
</form>
2、域对象(
推荐使用,也是最长使用的
UserBean很简单 ,此处略去,但有一点不得不说,在写好UserBean的时候最好从写一下
public int hashCode() 方法。
具体为什么还是没有搞懂,但应该要用到sort
)
public class TestActionSupport extends ActionSupport {
//此处user为一UserBean
public User user = new User();
@Override
public String execute() throws Exception {
System.out.println(user);
return super.execute();
}
}
<!--JSP 页面写法-->
<form action="/struts2-class/user/testActionSupport">
userId:<input type="text" name="user.userId"/><br/>
name:<input type="text" name="user.name" /><br/>
<input type="submit" />
</form>
3、模型驱动 implements ModelDriven(
优先被赋值:
当字段填充和模型驱动同时出现的时候模型驱动优先被执行。
)
//最好玩的就是这个ModelDriven ,被叫做模型驱动,他先需要被实现,最后执行一个
//getModel()的方法,返回这个UserBean的引用。并且一个Action只能对应一个ModelDriven
//神奇之处发生了,在JSP页面不需要写对象的引用了,直接写属性名就可以产生映射。
//原来不是很清楚,但能确定的是return的这个对象一定是到前台JSP页面去收集过一趟值//去了
public class TestActionSupport extends ActionSupport implements ModelDriven {
//此处user为一UserBean
public User user = new User();
//public String userId;
@Override
public String execute() throws Exception {
//System.out.println(userId);
System.out.println(user);
return super.execute();
}
//实现域模型 ModelDriven 要重写该方法
@Override
public Object getModel() {
// TODO Auto-generated method stub
//return 的值 传给了页面
return user;
}
}
<!--JSP页面-->
<form action="/struts2-class/user/testActionSupport">
userId:<input type="text" name="userId"/><br/>
name:<input type="text" name="name" /><br/>
age:<input type="text" name="age" /><br/>
<input type="submit" />
分享到:
相关推荐
struts2中 页面加载时动态填充下拉框的例子,用于向数据库请求数据进行填充。通过Action中的方法进行请求数据,来初始化Action中的属性,以便页面上的使用。包含jar包。
NULL 博文链接:https://ewf-momo.iteye.com/blog/1708697
jQuery EasyUI与Struts2 的整合—属性驱动篇 如果你是用模型驱动,或者是直接把属性写在action里面,你不会碰到我遇到的那么多问题。 但是你用模型驱动好吗,如果一个页面需要处理多个对象的时候怎么办。 如果你...
2. ActionSevlet首先调用struts配置文件(struts-config.xml),如果有表单提交,需要自动填充ActionForm Bean(找struts配置文件的),根据请求分派给相应的Action(找struts配置文件的<action-mappings>)。
Struts的工作原理,如下图2所示: <br/> 控制:通过图2大家可以看到有一个XML文件Struts-config.xml,与之相关联的是Controller,在Struts中,承担MVC中Controller角色的是一个Servlet,叫ActionServlet...
自定义模仿Struts2的框架,用配置文件配置转向的路径,可配置执行的方法名,自动提取表单的值填充到action,自动把action中的属性设到request的属性中 权限控制 字符编码过滤,对get请求同样有效果 自定义分页标签,...
parameter="" //指定Actgion的配置参数,在Action类的execute()方法中,可以调用ActionMapping对象的getParameter()方法来读取该配置参数。(相当于初始化赋值) >>> prefix=""// 指定填充当前 Action 关联 FormBean...
Struts框架的总控制器ActionServlet是一个Servlet,在web.xml中配置成自动启动的Servlet,在启动时总控制器会读取配置文件(struts-config.xml)的配置信息,为Struts中不同的模块初始化相应的对象。(面向对象思想) ...
这是一个简单的javaweb项目例子,使用的是struts2技术,在后台的action中写好一个list,然后给这个list填充好内容,之后在jsp前台显示,并可以通过导出到excel文件,存储在本地指定的位置。
1. Action建议统一遵守通配符的约定,basePath/web/moduleName/*_ *.action {1} EntityName,{2} ActionMethodName 2. 统一命名规则:接口类似UserService,接口实现类型UserServiceImpl;(IUserService和...
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class TextAction extends ActionSupport { /** */ private static final long serialVersionUID = 1...