JavaWeb_(Struts2框架)参数传递之接收参数与传递参数
此系列博文基于同一个项目已上传至github 传送门
JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门
JavaWeb_(Struts2框架)struts.xml核心配置、动态方法调用、结果集的处理 传送门
JavaWeb_(Struts2框架)Log4j的配置以及解决中文乱码 传送门
JavaWeb_(Struts2框架)参数传递之接收参数与传递参数 传送门
JavaWeb_(Struts2框架)Ognl小案例查询帖子 传送门
JavaWeb_(Struts2框架)Action中struts-default下result的各种转发类型 传送门
JavaWeb_(Struts2框架)拦截器interceptor 传送门
一、接收参数
第一种方法
【常用】1、继承implements ModelDriven<User>
- public class UserAction extends ActionSupport implements ModelDriven<User>{
2、创建一个对象,不要加get()和set()方法
- public User user = new User();
3、实现getModel()方法
- @Override
- public User getModel() {
- // TODO Auto-generated method stub
- return user;
- }
- package com.Gary.web;
- import org.apache.struts2.ServletActionContext;
- import com.Gary.domain.User;
- import com.Gary.service.UserService;
- import com.opensymphony.xwork2.ActionSupport;
- import com.opensymphony.xwork2.ModelDriven;
- public class UserAction extends ActionSupport implements ModelDriven<User>{
- public User user = new User();
- public String login() throws Exception {
- System.out.println("login()方法");
- System.out.println(user.getUsername());
- UserService userService = new UserService();
- boolean success = userService.findUser(user);
- if(success)
- {
- return "success";
- }else{
- ServletActionContext.getRequest().setAttribute("error", "用户名或密码错误!!!");
- return "error";
- }
- }
- //注册
- public String register() throws Exception {
- System.err.println("register()方法");
- return null;
- }
- //kill方法
- public String kill() throws Exception {
- System.err.println("kill()方法");
- return null;
- }
- @Override
- public User getModel() {
- // TODO Auto-generated method stub
- return user;
- }
- }
UserAction.java
第二种方法
1、添加属性驱动
- //添加属性驱动
- public String username;
- public String password;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- package com.Gary.web;
- import org.apache.struts2.ServletActionContext;
- import com.Gary.domain.User;
- import com.Gary.service.UserService;
- import com.opensymphony.xwork2.ActionSupport;
- import com.opensymphony.xwork2.ModelDriven;
- public class UserAction extends ActionSupport{
- //public User user = new User();
- //添加属性驱动
- public String username;
- public String password;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String login() throws Exception {
- System.out.println("login()方法");
- System.out.println(username+":"+ password);
- User user = new User();
- user.setUsername(username);
- user.setPassword(password);
- UserService userService = new UserService();
- boolean success = userService.findUser(user);
- if(success)
- {
- return "success";
- }else{
- ServletActionContext.getRequest().setAttribute("error", "用户名或密码错误!!!");
- return "error";
- }
- }
- //注册
- public String register() throws Exception {
- System.err.println("register()方法");
- return null;
- }
- //kill方法
- public String kill() throws Exception {
- System.err.println("kill()方法");
- return null;
- }
- }
UserAction.java
第三种方法
在login.jsp中修改登陆和注册标签框
- <label for="username" class="username_label"> 用 户 名 <input maxlength="20" name="user.username" type="text" placeholder="您的用户名和登录名" />
- </label>
- <label for="username" class="other_label"> 设 置 密 码 <input maxlength="20" type="password" name="user.password" placeholder="建议至少使用两种字符组合" />
- </label>
UserAction.java中直接通过user.getUsername()和user.setUsername()直接得到前台获取到的值
- public User user;
- public User getUser() {
- return user;
- }
- public void setUser(User user) {
- this.user = user;
- }
- public String login() throws Exception {
- System.out.println("login()方法");
- System.out.println(user.getUsername()+":"+ user.getPassword());
- UserService userService = new UserService();
- boolean success = userService.findUser(user);
- if(success)
- {
- return "success";
- }else{
- ServletActionContext.getRequest().setAttribute("error", "用户名或密码错误!!!");
- return "error";
- }
- }
- <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <link rel="stylesheet" href="css/head.css" />
- <link rel="stylesheet" type="text/css" href="css/login.css" />
- </head>
- <body>
- <div class="dvhead">
- <div class="dvlogo">
- <a href="index.html">你问我答</a>
- </div>
- <div class="dvsearch">10秒钟注册账号,找到你的同学</div>
- <div class="dvreg">
- 已有账号,立即 <a href="login.html">登录</a>
- </div>
- </div>
- <section class="sec">
- <form action="${pageContext.request.contextPath }/LoginAction_login" method="post">
- <div class="register-box">
- <label for="username" class="username_label"> 用 户 名 <input maxlength="20" name="user.username" type="text" placeholder="您的用户名和登录名" />
- </label>
- <div class="tips"></div>
- </div>
- <div class="register-box">
- <label for="username" class="other_label"> 设 置 密 码 <input maxlength="20" type="password" name="user.password" placeholder="建议至少使用两种字符组合" />
- </label>
- <div class="tips"></div>
- </div>
- <div class="arguement">
- <input type="checkbox" id="xieyi" /> 阅读并同意 <a href="javascript:void(0)">《你问我答用户注册协议》</a> <a href="register.html">没有账号,立即注册</a>
- <div class="tips" style="color: red">${error}</div>
- </div>
- <div class="submit_btn">
- <button type="submit" id="submit_btn">立 即 登录</button>
- </div>
- </form>
- </section>
- <script src="js/index.js" type="text/javascript" charset="utf-8"></script>
- </body>
login.jsp
- package com.Gary.web;
- import org.apache.struts2.ServletActionContext;
- import com.Gary.domain.User;
- import com.Gary.service.UserService;
- import com.opensymphony.xwork2.ActionSupport;
- import com.opensymphony.xwork2.ModelDriven;
- public class UserAction extends ActionSupport{
- public User user;
- public User getUser() {
- return user;
- }
- public void setUser(User user) {
- this.user = user;
- }
- public String login() throws Exception {
- System.out.println("login()方法");
- System.out.println(user.getUsername()+":"+ user.getPassword());
- UserService userService = new UserService();
- boolean success = userService.findUser(user);
- if(success)
- {
- return "success";
- }else{
- ServletActionContext.getRequest().setAttribute("error", "用户名或密码错误!!!");
- return "error";
- }
- }
- //注册
- public String register() throws Exception {
- System.err.println("register()方法");
- return null;
- }
- //kill方法
- public String kill() throws Exception {
- System.err.println("kill()方法");
- return null;
- }
- }
UserAction.java
二、传递参数
在ImplAction.java中通过login()方法添加<key,value>到session域和application域中
- public String login() throws Exception
- {
- //得到原生的request域
- //ServletActionContext.getRequest().setAttribute("username", "123");
- //ServletActionContext.getRequest().setAttribute("password", "123");
- ActionContext.getContext().put("username", "123");
- ActionContext.getContext().put("password", "123");
- //获得session
- Map<String,Object> session = ActionContext.getContext().getSession();
- session.put("mysession", "这是session域");
- //获得application
- Map<String,Object> application = ActionContext.getContext().getApplication();
- application.put("myapplication", "这是application域");
- //获得原生的request
- HttpServletRequest request = ServletActionContext.getRequest();
- //获得原生的response
- HttpServletResponse response = ServletActionContext.getResponse();
- return "toLogin";
- }
struts.xml中动态方法配置了action,将web层ImplAction中的login()转发到com.Gary.web.UserAction中的login()方法
- <action name="LoginAction_*" class="com.Gary.web.UserAction" method="{1}">
- <!-- 默认为转发 redirect设置为重定向 -->
- <result name="success" type="redirect">/index.html</result>
- <!-- 默认为转发 -->
- <result name="error">/login.jsp</result>
- </action>
- <action name="LoginActionImpl_*" class="com.Gary.web.ImplAction" method="{1}">
- <!-- 转发到LoginActionDefault -->
- <result name="defaultAction" type="chain">LoginActionDefault</result>
- <!-- 重定向到Action(LoginAction_*) -->
- <result name="toLogin" type="redirectAction">
- <param name="actionName">LoginAction_login</param>
- <param name="username">${username}</param>
- <param name="password">${password}</param>
- </result>
- </action>
在UserAction.java中输出放置在session域和application域中的内容
- public String login() throws Exception {
- System.out.println("login()方法");
- System.out.println(user.getUsername()+":"+user.getPassword());
- System.out.println("session域..."+ActionContext.getContext().getSession().get("mysession"));
- System.out.println("application域..."+ActionContext.getContext().getApplication().get("myapplication"));
- UserService userService = new UserService();
- boolean success = userService.findUser(user);
- if(success)
- {
- return "success";
- }else{
- ServletActionContext.getRequest().setAttribute("error", "用户名或密码错误!!!");
- return "error";
- }
- }
- package com.Gary.web;
- import java.util.Map;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.Action;
- import com.opensymphony.xwork2.ActionContext;
- public class ImplAction implements Action{
- @Override
- public String execute() throws Exception {
- System.out.println("这是实现了Action接口的action");
- return "defaultAction";
- }
- public String login() throws Exception
- {
- //得到原生的request域
- //ServletActionContext.getRequest().setAttribute("username", "123");
- //ServletActionContext.getRequest().setAttribute("password", "123");
- ActionContext.getContext().put("username", "123");
- ActionContext.getContext().put("password", "123");
- //获得session
- Map<String,Object> session = ActionContext.getContext().getSession();
- session.put("mysession", "这是session域");
- //获得application
- Map<String,Object> application = ActionContext.getContext().getApplication();
- application.put("myapplication", "这是application域");
- //获得原生的request
- HttpServletRequest request = ServletActionContext.getRequest();
- //获得原生的response
- HttpServletResponse response = ServletActionContext.getResponse();
- return "toLogin";
- }
- }
ImplAction.java
- <?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>
- <constant name="struts.devMode" value="true"></constant>
- <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
- <!-- name:配置包名 namespace:给action的访问路径定义一个命名空间 -->
- <package name="MyPackage" namespace="/" extends="struts-default">
- <!-- 增加动态方法调用的安全性 -->
- <global-allowed-methods>regex:.*</global-allowed-methods>
- <!-- action:配置action类
- name:决定了action访问的资源名称 servlet:url-pattern
- class:action的完整类名
- method:指定调用action中的哪个方法来去处理请求 -->
- <action name="LoginAction_*" class="com.Gary.web.UserAction" method="{1}">
- <!-- 默认为转发 redirect设置为重定向 -->
- <result name="success" type="redirect">/index.html</result>
- <!-- 默认为转发 -->
- <result name="error">/login.jsp</result>
- </action>
- <action name="LoginActionDefault" class="com.Gary.web.DefaultAction" method="execute">
- </action>
- <action name="LoginActionImpl_*" class="com.Gary.web.ImplAction" method="{1}">
- <!-- 转发到LoginActionDefault -->
- <result name="defaultAction" type="chain">LoginActionDefault</result>
- <!-- 重定向到Action(LoginAction_*) -->
- <result name="toLogin" type="redirectAction">
- <param name="actionName">LoginAction_login</param>
- <param name="username">${username}</param>
- <param name="password">${password}</param>
- </result>
- </action>
- </package>
- </struts>
struts.xml
- package com.Gary.web;
- import org.apache.struts2.ServletActionContext;
- import com.Gary.domain.User;
- import com.Gary.service.UserService;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- import com.opensymphony.xwork2.ModelDriven;
- public class UserAction extends ActionSupport implements ModelDriven<User>{
- public User user = new User();
- public String login() throws Exception {
- System.out.println("login()方法");
- System.out.println(user.getUsername()+":"+user.getPassword());
- System.out.println("session域..."+ActionContext.getContext().getSession().get("mysession"));
- System.out.println("application域..."+ActionContext.getContext().getApplication().get("myapplication"));
- UserService userService = new UserService();
- boolean success = userService.findUser(user);
- if(success)
- {
- return "success";
- }else{
- ServletActionContext.getRequest().setAttribute("error", "用户名或密码错误!!!");
- return "error";
- }
- }
- //注册
- public String register() throws Exception {
- System.err.println("register()方法");
- return null;
- }
- //kill方法
- public String kill() throws Exception {
- System.err.println("kill()方法");
- return null;
- }
- @Override
- public User getModel() {
- // TODO Auto-generated method stub
- return user;
- }
- }
UserAction.java
JavaWeb_(Struts2框架)参数传递之接收参数与传递参数的更多相关文章
- JavaWeb_(Struts2框架)拦截器interceptor
此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...
- JavaWeb_(Struts2框架)Action中struts-default下result的各种转发类型
此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...
- JavaWeb_(Struts2框架)Ognl小案例查询帖子
此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...
- JavaWeb_(Struts2框架)Log4j的配置以及解决中文乱码
此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...
- JavaWeb_(Struts2框架)struts.xml核心配置、动态方法调用、结果集的处理
此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...
- JavaWeb_(Struts2框架)Struts创建Action的三种方式
此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...
- JavaWeb_(Struts2框架)使用Struts框架实现用户的登陆
JavaWeb_(Struts2框架)使用Servlet实现用户的登陆 传送门 JavaWeb_(Struts2框架)Servlet与Struts区别 传送门 MySQL数据库中存在Gary用户,密码 ...
- JavaWeb_(Struts2框架)使用Servlet实现用户的登陆
JavaWeb_(Struts2框架)使用Struts框架实现用户的登陆 传送门 JavaWeb_(Struts2框架)Servlet与Struts区别 传送门 MySQL数据库中存在Gary用户,密 ...
- 17_Android中Broadcast详解(有序广播,无序广播)最终广播,Bundle传递参数,传递参数的时候指定权限
1 Broadcast是Android中的四大组件之一,他的用途很大,比如系统的一些广播:电量低.开机.锁屏等一些操作都会发送一个广播. 2 广播被分为两种不同的类型:"普通广播( ...
随机推荐
- 怎样创建并使用 vue 组件 (component) ?
组件化开发 需要使用到组件, 围绕组件, Vue 提供了一系列功能方法, 这里仅记录组件的 最简单 的使用方法. 1. 通过 Vue.component(tagName, options) 注册一个 ...
- Fullscreen API:全屏操作
function launchFullscreen(element) { if(element.requestFullscreen) { element.requestFullscreen(); } ...
- golang(8):channel读写 & goroutine 通信
goroutine 1.进程和线程 A. 进程是程序在操作系统中的一次执行过程,系统进行资源分配和调度的一个独立单位 B. 线程是进程的一个执行实体,是CPU调度和分派的基本单位,它是比进程更小的能独 ...
- JS基础_while循环
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- element-ui中关闭对话框清空验证,清除form表单数据
对于elementUI中对话框,点击对话框和关闭按钮 怎么清空验证,清空form表单,避免二次点击还会有 验证错误的提示.今天终于自己查资料解决了,分享给大家 1.首先在你的对话框 取消按钮 加一个c ...
- node jade || ejs引擎模板
1.jade:破坏式2.ejs:保留式 -------------------------------------------------------------------------------- ...
- IIS 6.0 PUT上传 任意文件创建漏洞
IIS 6.0 PUT上传 任意文件创建漏洞 require 1.IIS Server在Web服务扩展中开启了WebDAV. 2.IIS配置了可以写入的权限,包括网站 1.根目录 2.子文件夹 3.i ...
- 3.移动端自动化测试-appium环境搭建(原理)
appium自动化原理: 需要服务端(appium启动),手机端(adb连接设备),脚本端(pycharm)就可以进行 自己总结下: 手机和脚本连接:1.adb连接,2靠脚本导入驱动. 脚本和服务端连 ...
- SokcetClient c++
#include "pch.h" #include "SokcetClient.h" #include <iostream> #include &l ...
- Java面向对象(一)
面向对象(Object Oriented) 面向过程:事物比较简单.将问题分解为若干个步骤.按照步骤依次执行.面向对象:事物比较复杂.在解决面向对象的过程中,最后的执行部分还是面向过程方式,面向过程和 ...