struts2 的3种驱动 第三种方式 模型驱动
[size=x-large]第三种是:将对象写在action中 action实现一个ModelDriven :[/size] [color=red]在action中 我只写了一个user 对象 : action 中的内容:[/color] package com.luhua.test; import com.luhua.pojo.User; import com.opensymphony.xwork2.ModelDriven; public class UserAction implements ModelDriven{ //用户 private User user = new User(); public String user(){ System.out.println(user.getUsername()); return "success" ; } public String execute(){ return "success" ; } public User getModel() { return user; } } [color=red]user类中:只有一个username的属性,并提供了get和set方法[/color] package com.luhua.pojo; import java.io.Serializable; public class User implements Serializable { private static final long serialVersionUID = 1L; public User(){} private String username ; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } } [color=red]在struts.xml中的配置是:[/color] index.jsp success.jsp [color=red]jsp页面有两张,index.jsp 和 success.jsp 从index.jsp中输入用户名; 在action中打印出来[/color] [color=red]页面的输入的name必须是属性名,不能用对象名.属性名[/color] [color=red]index.jsp:[/color] <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
username:
[color=red]success.jsp[/color] <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> user