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; } }
第二种方法
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; } }
第三种方法
在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>
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; } }
二、传递参数
在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"; } }
<?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>
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; } }