jsp与action之间的传值

jsp向action传参

1. action里未声明的值,ServletActionContext.getRequest().getParameter("username")
2. action里已经声明并getter和setter的值,可以直接使用或get

action向jsp传值

  1. action里未声明的值,ServletActionContext.getRequest().setAttribute(“username”, username);
  2. action里已经声明并getter和setter的值,set方法

action代码段

public class UserAction extends BaseAction {

    private UserService userService;

    private User user;
    private String message;
    private List<User> queryList;
    private List<Department> departments;

    public String login() {
        System.out.println(user);
        User user1 = userService.findUserByName(user);
        if (null == user1) {
            setMessage("用户名不存在!");
            return Action.ERROR;
        }
        if (!user.getPassword().equals(user1.getPassword())) {
            setMessage("密码错误!");
            return Action.ERROR;
        } else {
            setQueryList(userService.findAll());
            setDepartments(departmentService.findAll());
            return Action.SUCCESS;
        }
    }

    getter and setter...
}

jsp关键代码段

<body>
    <form name="loginform" action="login.action" method="post">
        用户:<input type="text" name="user.userName" /> 
        密码:<input type="password" name="user.password" /> 
        <input type="submit" value="登录" />
    </form>
    ${message}
</body>

Struts代码段

<action name="login" class="com.dirk.action.UserAction" method="login">
    <result name="success">/WEB-INF/jsp/department/userManage.jsp</result>
    <result name="error">/WEB-INF/jsp/login/login.jsp</result>
</action>

猜你喜欢

转载自blog.csdn.net/shepherd_dirk/article/details/80354034