Struts2 学习笔记 2
来源:互联网 发布:淘宝评论上传不了图片 编辑:程序博客网 时间:2024/05/16 06:58
一、首先介绍下讲解的内容
1、修改请求后缀的三种方式。
2、获取request,response,session,applicationContext的三种方式
二、在第一个项目学习的基础上进行讲解,开始
修改后缀的三种方式
1、在类路径根目录下新建struts.properties文件,文件内容如下
struts.action.extension=action,zhaohan
2、修改struts.xml
<struts><!-- 第二种更改default.properties文件的方式这种方式在多数工程中普遍使用--><constant name="struts.action.extension" value="action,zhaohan"></constant></struts>
3、修改web.xml
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> <!-- 3:这是第三种覆盖 default.properties文件参数的方式 --> <init-param> <param-name>struts.action.extension</param-name> <param-value>action,zhaohan</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
注意:
修改default.properties文件时
优先级高低问题
default.properties(源码)
struts-default.properties(源码)
struts-plugin.xml(源码)
struts.xml
struts.properties
web.xml
注意出现冲突时后加载的会覆盖之前加载的
获取request,response,session,applicationContext的三种方式
1、在包com.etoak.action路径下新建RegAction1.java内容为:(里面包含第一种获取的讲解)
package com.etoak.action;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;/* * 一般不继承任何类就可以完成业务逻辑,但是 * 官方推荐我们继承一个与servlet没有任何耦合的工具类 * 来帮助我们完成业务逻辑以及验证操作 * ActionSupport * * */public class RegAction1 extends ActionSupport {private String myname;private String mypass;public String getMyname() {return myname;}public void setMyname(String myname) {this.myname = myname;}public String getMypass() {return mypass;}public void setMypass(String mypass) {this.mypass = mypass;}@Overridepublic String execute() throws Exception {/*1:耦合servlet * 使用Servlet提供的几种老范围进行业务逻辑 * 工具类:ServletActionContext * */HttpServletRequest request = ServletActionContext.getRequest();request.setAttribute("n",this.getMyname());HttpSession session = request.getSession();session.setAttribute("p",this.getMypass());ServletContext application = ServletActionContext.getServletContext();application.setAttribute("w", "welcome reg~~~");/* * 返回一个字符串即可但是官方为了提高 * 安全性为我们设置了5个静态常量,这五个常量 * 我们在继承ActionSupport之后直接可以使用 * SUCCESS ERROR LOGIN NONE INPUT * */return this.SUCCESS;}@Overridepublic void validate() {// 使用字段级验证if (this.getMyname().trim().length() < 4|| this.getMyname().trim().length() > 8) {//添加字段级错误提示信息/* * 所谓字段级是指哪个表单项出错就从页面哪个 * 表单项出显示错误提示信息 * * 第一个参数对应页面表单项 * <s:text name="对应这里" /> * 第二个参数表示错误提示信息是什么 * */this.addFieldError("myname","用户名不能小于4位大于8位");}/* * 此方法由ActionProxy初始化验证拦截器提供 * 由ActionInvocation为这个Action调用 * 如果出现错误这个拦截器自动返回input * 字符串,用户不需要做出任何操作,仅仅需要 * 调用结果集接受返回的input字符串 * */}}
2、在包com.etoak.action路径下新建RegAction2.java内容为:(里面包含第二种获取的讲解)
package com.etoak.action;import java.util.Map;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class RegAction2 extends ActionSupport {private String myname;private String mypass;public String getMyname() {return myname;}public void setMyname(String myname) {this.myname = myname;}public String getMypass() {return mypass;}public void setMypass(String mypass) {this.mypass = mypass;}@Overridepublic String execute() throws Exception {/* * 使用Struts2框架中为我们提供的几种取代 * Servlet四范围的新的范围 * 使用这几种范围需要使用工具类 * ActionContext * *///ActionContext本身就取代了request范围ActionContext ac = ActionContext.getContext();ac.put("n",myname);//使用新的session范围Map<Object,Object> session = ActionContext.getContext().getSession();session.put("p",mypass);//使用新的application范围Map<Object,Object> application = ActionContext.getContext().getApplication();application.put("w","welcome reg~~");return this.SUCCESS;}@Overridepublic void validate() {// 使用类级别验证if (this.getMyname().trim().length() < 4|| this.getMyname().trim().length() > 8) {/* * 注意:类级别的验证表示任何表单项出现错误之后 * 都从页面的<s:actionerror>处显示错误提示信息 * * */this.addActionError("用户名不能小于4位大于8位");}/* * 此方法由ActionProxy初始化验证拦截器提供 * 由ActionInvocation为这个Action调用 * 如果出现错误这个拦截器自动返回input * 字符串,用户不需要做出任何操作,仅仅需要 * 调用结果集接受返回的input字符串 * */}}
3、在包com.etoak.action路径下新建RegAction3.java内容为:(里面包含第三种获取的讲解)
package com.etoak.action;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.interceptor.ServletRequestAware;import org.apache.struts2.interceptor.ServletResponseAware;import org.apache.struts2.util.ServletContextAware;import com.opensymphony.xwork2.ActionSupport;public class RegAction3 extends ActionSupportimplements ServletRequestAware,ServletResponseAware,ServletContextAware{private String myname;private String mypass;private HttpServletRequest request;private HttpServletResponse response;private ServletContext application;/* * 通过注入的方式使用setter将 * 几个老范围全部引入到本类中 * */@Overridepublic void setServletContext(ServletContext application) {this.application = application;}@Overridepublic void setServletResponse(HttpServletResponse response) {this.response = response;}@Overridepublic void setServletRequest(HttpServletRequest request) {this.request = request;}public String getMyname() {return myname;}public void setMyname(String myname) {this.myname = myname;}public String getMypass() {return mypass;}public void setMypass(String mypass) {this.mypass = mypass;}@Overridepublic String execute() throws Exception {request.setAttribute("n", myname);request.getSession().setAttribute("p",mypass);application.setAttribute("w","welcome reg");return this.SUCCESS;}}
三、修改前台页面,使项目跑起来
1,index.jsp页面的修改
<body> <%-- 修改default.properties文件时 优先级高低问题 default.properties(源码) struts-default.properties(源码) struts-plugin.xml(源码) struts.xml struts.properties web.xml 注意出现冲突时后加载的会覆盖之前加载的 --%><s:form action="reg.zhaohan"><%--任意表单项都从此标签处显示错误提示信息--%><s:actionerror/><s:textfield label="用户姓名" name="myname"cssStyle="width:138px"></s:textfield><s:password label="用户密码" name="mypass"cssStyle="width:138px"></s:password><s:submit value="注册"></s:submit><s:reset value="取消"></s:reset></s:form> </body>
2、suc.jsp页面的修改
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title></title> </head> <body><%=request.getAttribute("n") %><%=session.getAttribute("p") %><%=application.getAttribute("w") %><br><c:out value="${n}"></c:out><c:out value="${p}"></c:out><c:out value="${w}"></c:out><br><%-- 注意#范围 任意情况下不准删除 --%><s:property value="#request.n" /><s:property value="#session.p" /><s:property value="#application.w" /> </body></html>
3、struts.xml的修改
<package name="pro" extends="struts-default"> <!-- 修改class=""下的类路径就可以访问新建的那三个类 --> <action name="reg" class="com.etoak.action.RegAction3"> <!-- name:如果返回的是success可以省略不写 type:如果省略不写表示请求转发 --> <result>/suc.jsp</result> <!-- 验证未通过的话自动返回input字符串 --> <result name="input">/index.jsp</result> </action> </package>
完。。。
0 0
- struts2学习笔记(2)
- struts2学习笔记2
- Struts2学习笔记(2)
- Struts2学习笔记(2)
- Struts2 学习笔记 2
- Struts2学习笔记(2)
- Struts2学习笔记(2)
- Struts2学习笔记(2)
- struts2学习笔记(2)
- struts2学习笔记--struts2配置
- Struts2学习笔记--安装struts2
- Struts2学习笔记4:Struts2的类型转换【2】
- Struts2学习笔记(2)-----Struts2注释(下)
- Struts2学习笔记(2)——Struts2基本配置
- Struts2学习笔记(2)——Struts2体系结构
- Struts2的学习笔记(2)
- Struts2学习笔记2--输入校验
- struts2学习笔记2--常量定义
- 使用PHP实现二分查找算法代码分享
- 配置完jdk后所有指令都不能用了该怎么破?
- 欢迎使用CSDN-markdown编辑器
- iOS开发基础之开发证书的说明和发布
- Oracle 创建普通用户,并赋予权限
- Struts2 学习笔记 2
- C/C++ 用libcurl库进行http通讯网络编程
- android读写串口的权限问题
- 长方体数
- 关于linux系统的日志文件的学习与理解
- 解决-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variabl
- Python字典按值排序、包含字典的列表按字典值排序的方法
- windows7 64位下git和tortoisegit的安装和使用
- latex 常用环境(environment)