Struts入门(三)深入Struts用法讲解

  1. 访问Servlet API
  2. Action搜索顺序
  3. 动态方法调用
  4. 指定多个配置文件
  5. 默认Action
  6. Struts 后缀
  7. 接收参数
  8. 处理结果类型

1.访问Servlet API

   首先我们了解什么是Servlet API 

  httpRequest、httpResponse、servletContext
  3个api对应jsp面向对象:request、response、application
  servlet中可以直接调用servlet api


  struts2 Action中execute没有任何参数,也就是不存在servlet api 

   Struts2框架底层是基本Servlet的,所以我们肯定要去访问Servlet API,而且开发Web应用不去访问Servlet API也是不可能的,

所以我们Struts2框架提供了我们去访问Servlet API的方法;            

struts2 提供了3种方式访问servlet api:       

   ①:使用ServletActionContext访问Servlet API;    ActionContext类   

   ②:使用ActionContext访问ServletAPI;              ServletActionCotext类

   ③:使用一些接口 如 ServletRequestAwa...;          实现***Aware接口

  

2.Action搜索顺序

 

我们来看一个路径:

我们这里新建一个student.action 没有命名空间那么我们访问的路径就是

http://localhost:8080/ProjectName(项目的名字)/student.action

那么我们改成下面的路径

 http://localhost:8080/ProjectName(项目的名字)/path1/path2/path3/student.action

 在浏览器中访问也能访问到正确的页面

 

因此我们可以看出访问的顺序是从文件的上级 也就是最后一级包开始找

 http://localhost:8080/ProjectName(项目的名字)/path1/path2/path3/student.action

 http://localhost:8080/ProjectName(项目的名字)/path1/path2/student.action

 http://localhost:8080/ProjectName(项目的名字)/path1/student.action

 http://localhost:8080/ProjectName(项目的名字)/student.action

 从path3 一直到path1都找不到 最后在根目录下找到  如果找不到就会报错了  

  这就是action的搜索顺序!

3.动态方法调用

   在.net MVC中 我们在Controller中创建一个一个方法  只要在页面中指定相应的mvc路径  我们视图的一个url就能请求得到

在struts中 我们则需要手工进行配置  指定页面和后台方法的匹配

  这里的动态方法调用就是为了解决一个Action对应多个请求得处理。以免Action太多(像指定method属性的配置方式  )

  动态调用有三种方式  这里指定method属性和感叹号方式(不推荐使用)不再说明   我们来说下常用的通配符方式:

首先在struts.xml配置我们的参数

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" >
<struts><package name="default" namespace="/" extends="struts-default"><action name="HelloWorld2" class="com.HelloWorldAction"><result>/result.jsp</result></action><!-- name 代表我们的action名也就是url里面的名字   class是指定我们的后台类文件method {1} 与name中的*对应--><action name="helloworld_*"  method="{1}" class="com.HelloWorldAction"><result >/result.jsp</result><result name="add" >/add.jsp</result><result  name="update" >/update.jsp</result></action></package>
</struts>
View Code

这里我们需要创建三个jsp文件  默认路径的result.jsp   还有add方法的add.jsp   update方法的update.jsp

 

 

页面里面我们都用一句话简单区分  这样 启动Debug as Server 然后在浏览器中访问就可以对应到相应的路径了

 

这里struts.xml文件result标签值都改成{1}.jsp  一样的道理    这里可以随意加参数进行配置

4.指定多个配置文件

 如果项目比较大 则需要比较多的配置  我们在入门(二)文件中看到注释可以用include来包含多个配置文件

<include file="***.xml"> </include>
<constant name="struts.i18n.encoding" value="UTF-8"> </constant>
ps:
1.被include的标签一定要符合struts的dtd规范。也就是说被include的xml文件的内部格式要符合struts的xml文件规范(跟struts.xml一摸一样)。
2.xml文件的编码格式要相同,如果是utf-8,那么都是utf-8。

5.默认Action

   默认action是为了改善用户体验,当用户输入的URL找不到对应的action,就会使用默认Action

 

 

【找不到默认action的原因和解决方法】
<default-action-ref name="index"></default-action-ref><action name="index"><result>/error.jsp</result></action>  <br><action name="log_*" method="{1}"  class="com.wayne.action.LoginAction"><result name="login">/login.jsp</result><result name="logout">/logout.jsp</result></action>

  将上面代码放到package标签中 这里定义了一个index的默认标签  

通配符会覆盖掉默认action,所以不能有【*_*】这样子的action,要改成【log_*_*】这类型的命名,
否则,【*_*】里面的第一个*就包括了所有的字符,直接进入了这个action进行处理,无法进入默认的action了。

 6.Struts 后缀

 

 三种方式:

1.struts.properties中:struts.action.extension=action,do,struts2
2.struts.xml中增加常量constant: <constant name="struts.action.extension" value="action,do,struts2"></constant>
3.在web.xml过滤器中配置intt-param参数: <init-param><param-name>struts.action.extension</param-name><param-value>do,action,strtus2</param-value> </init-param>

 7.接收参数

   之前我们说到struts项目中  我们写了前台jsp页面  写了后台action页面  那么要想两者进行关联就需要在配置文件中配置关联关系(真是麻烦。)

下面我们用项目示例来说明下接收参数问题:

 

  首先我们建立一个login.jsp页面

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <form action="LoginAction.action" method="post">
11         用户名:<input type="text" name="username" />
12         密码:<input type="password" name="password" />
13         <input type="submit"  value="提交"/>
14     </form>
15 </body>
16 </html>
View Code

 然后我们创建一个后台Class   LoginAction.java

package com;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport {public String login(){return SUCCESS;}
}
View Code

 

然后我们在struts.xml配置文件中配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" >
<struts><package name="default" namespace="/" extends="struts-default"><action name="HelloWorld2" class="com.HelloWorldAction"><result>/result.jsp</result></action><action name="LoginAction"  method="login" class="com.LoginAction"><result >/success.jsp</result> </action></package>
</struts>
View Code

 

这里添加了一个过滤器 指定了访问LoginAction的login方法

 然后我们如何通过action的属性获取form提交的值

第一种方式:直接在action类中实现--使用Action的属性接收参数(不利于面向对象)

 1 package com;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 public class LoginAction extends ActionSupport {
 6     //创建两个属性的get set方法
 7     private String username;
 8     private String password;
 9     
10      public String login(){
11          System.out.println(username);
12          return SUCCESS;
13      }
14 
15     public String getUsername() {
16         return username;
17     }
18 
19     public void setUsername(String username) {
20         this.username = username;
21     }
22 
23     public String getPassword() {
24         return password;
25     }
26 
27     public void setPassword(String password) {
28         this.password = password;
29     }
30      
31      
32 }
View Code

 

然后访问:http://localhost:8080/StrutsDemo/login.jsp  输入用户名密码  提交   会发现控制台中打印输出了 用户名

 

第二种方式:使用DomainModel接收参数

  这里要把第一种属性声明的方式 单独放到一个类中去

  建立一个User类

  

 1 package com.po;
 2 
 3 public class User {
 4     //创建两个属性的get set方法
 5         private String username;
 6         private String password;
 7         
 8         public String getUsername() {
 9             return username;
10         }
11 
12         public void setUsername(String username) {
13             this.username = username;
14         }
15 
16         public String getPassword() {
17             return password;
18         }
19 
20         public void setPassword(String password) {
21             this.password = password;
22         }
23 }
View Code

  然后action中去掉属性相关 声明一个user类

package com;import com.opensymphony.xwork2.ActionSupport;
import com.po.User;public class LoginAction extends ActionSupport {public User getUser() {return user;}public void setUser(User user) {this.user = user;}private User user;public String login(){System.out.println(user.getUsername());return SUCCESS;} }
View Code

 

 

login.jsp页面中需要更改name的值

<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body><form action="LoginAction.action" method="post">用户名:<input type="text" name="user.username" />密码:<input type="password" name="user.password" /><input type="submit"  value="提交"/></form>
</body>
</html>
View Code

 

然后再次启动下  访问:http://localhost:8080/StrutsDemo/login.jsp  输入用户名密码  提交   会发现控制台中同样打印输出了 用户名

第三种方式:使用ModelDriven接收参数(推荐方式)

这里我们需要实现ModelDriven接口  

package com;import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.po.User;public class LoginAction extends ActionSupport 
implements ModelDriven<User>{private User user=new User(); //这里需要实例化 去掉了 get set方法  实现了ModelDriven的方法public String login(){System.out.println(user.getUsername());return SUCCESS;}@Overridepublic User getModel() {// TODO Auto-generated method stubreturn user;} }
View Code

 

这里我们也不再需要指定jsp中的name 对象  去掉user.

<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body><form action="LoginAction.action" method="post">用户名:<input type="text" name="username" />密码:<input type="password" name="password" /><input type="submit"  value="提交"/></form>
</body>
</html>
View Code

 

 

如果我们传递一个List方式一样的道理  在jsp中 我们声明

书籍1:<input type="text"  name="BookList[0]"/>
书籍2:<input type="text" name="BookList[1]"/>

 

后台User类中我们创建List   BookList对象

private List<String> BookList;public List<String> getBookList() {return BookList;}public void setBookList(List<String> bookList) {BookList = bookList;}
View Code

 

这里我们就简单介绍这几种方式  最后做一个总结:
 1 接收参数
 2 1,使用Action的属性接受参数,在Action中定义需要接受的属性,并写它的set/get方法。
 3 2,使用DomainModel接受参数,创建实体类定义需要接受的属性,并set/get方法,在Action中创建实体类名属性。并在界面进行指定。
 4 3,使用ModelDriver接受参数,在Action中实现ModelDriver<实体类名>接口,并实现方法返回当前需要转换的对象,删除set/get方法,并对 对象 进行实例化,并取消指定。
 5 4,request
 6 5,获取List集合中的参数。获取多个参数。
 7 
 8 第一种接收参数的方法:直接在action类中创建相应的属性和getter和setter,和前端的name名字相同。eg:前端的username,在action类中就要建立一个private String username; Struts会自动映射为这个属性赋值
 9 
10 第二种接受参数的方法:使用DomainModel,将username 和password两个属性封装为一个类User(必须是标准的JavaBean),在action中声明这个属性:private User user; 同时为user设置getter和setter;在前端中的name需要设置为user.name和user.password,才能映射成功
11 
12 第三种接收参数的方法:使用ModelDriven<T>接口,这个action必须实现这个接口的public T getModel()方法。此时声明的属性必须实例化,eg: private User user = new User(); 同时不需要getter和setter。前端的name也只需要写username和password就可以,不需要再加域了。这种方法时最推荐的方法,因为可以减少前后端的耦合

 

8.处理结果类型

 

 我们来看一下struts.xml中过滤器的一句话

<result name="success">/result.jsp</result>  这句话等同于<result >/result.jsp</result>  说明name的默认值就是success
【Structs2处理流程】
用户请求Structs框架控制器(Action)Structs框架视图资源 返回String,提供代码复用性,有利于框架分离。 【Action中五种内置属性(com.opensymphony.xwork2.Action)】

 



转载于:https://www.cnblogs.com/DemoLee/p/6291341.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/402283.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux errno定义

以下内容来自于Ubuntu系统&#xff0c;请看执行情况&#xff1a; [zcmasm-generic #6]$pwd /usr/include/asm-generic [zcmasm-generic #7]$ls errno* -lh -rw-r--r-- 1 root root 1.6K Jun 19 2013 errno-base.h -rw-r--r-- 1 root root 5.2K Jun 19 2013 errno.h [zcmasm-g…

linux多线程编程——同步与互斥

一、 为什么要用多线程技术&#xff1f; 1、避免阻塞&#xff0c;大家知道&#xff0c;单个进程只有一个主线程&#xff0c;当主线程阻塞的时候&#xff0c;整个进程也就阻塞了&#xff0c;无法再去做其它的一些功能了。 2、避免CPU空转&#xff0c;应用程序经常会涉及到RPC&am…

黑马程序员_泛型

--------------------ASP.NetAndroidIOS开发、.Net培训、期待与您交流&#xff01; -------------------- 1. 泛型 1.概述 泛型是为了解决了集合中存储对象安全问题&#xff0c;如果集合中存数了不同类型的对象&#xff0c;那么读取出来后&#xff0c;操作取出的对象以为不…

菜鸟成长记(十一)----- 操蛋的2016与未知的2017

现在已经2017.1.16号了&#xff0c;早就说着要写篇总结&#xff0c;骂骂特么操蛋的自己&#xff0c;当然这两三年来在这里骂的真特么不在少数了&#xff0c;但是都是特么一拖再拖&#xff0c;刚刚明明是在看TPO阅读的&#xff0c;但是特么实在是无法集中精神的看&#xff0c;作…

VS.NET版本与VC版本对应关系

vc6 -> vc6vs2003 -> vc7vs2005 -> vc8vs2008 -> vc9vs2010 -> vc10vs2012 -> vc11vs2013 -> vc12仅供参考&#xff01;

sql2008 获取表结构说明

SELECT 表名 case when a.colorder1 then d.name else end, 表说明 case when a.colorder1 then isnull(f.value,) else end, 字段序号 a.colorder, 字段名 a.name, 标识 case when COLUMNPROPERTY( a.id,a.name,IsIdentity)1 th…

Linux ALSA声卡驱动之四:Control设备的创建

声明&#xff1a;本博内容均由http://blog.csdn.net/droidphone原创&#xff0c;转载请注明出处&#xff0c;谢谢&#xff01; Control接口 Control接口主要让用户空间的应用程序&#xff08;alsa-lib&#xff09;可以访问和控制音频codec芯片中的多路开关&#xff0c;滑动控件…

【linux】信号量的值定义

参见文件&#xff1a;/usr/include/bits/signum.h /* Signal number definitions. Linux version.Copyright (C) 1995-2013 Free Software Foundation, Inc.This file is part of the GNU C Library.The GNU C Library is free software; you can redistribute it and/ormodi…

汇编学习笔记-序章

最近突然对汇编语言开始感兴趣&#xff0c;于是说干就干了。 之前也自学过一点汇编&#xff0c;是跟着王爽老师的《汇编语言(第3版) 》这本书学习的&#xff0c;已经是有5 6前年的样子了。当时觉得这本书写的非常通俗易懂是一本非常好的启蒙书籍&#xff0c;但是最近在翻阅的时…

jQuery 入门教程(5): 显示/隐藏内容

2019独角兽企业重金招聘Python工程师标准>>> jQuery的hide()和show()可以用来显示和隐藏内容。比如下面的例子&#xff1a;jQuery的hide()和show() 可以用来显示和隐藏内容。比如下面的例子&#xff1a; [html] view plain copy print ? <!doctype html> …

键盘键值表

键盘键值表 值 描述 0x1 鼠标左键 0x2 鼠标右键 0x3 CANCEL 键 0x4 鼠标中键 0x8 BACKSPACE 键 0x9 TAB 键 0xC CLEAR 键 0xD ENTER 键 0x10 SHIFT 键 0x11 CTRL 键 0x12 MENU 键 0x13 PAUSE 键 0x14 CAPS LOCK 键 0x1B ESC 键 0x20 SPACEBAR 键 0x21 PAGE UP 键 0x22 PAGE DOW…

Django QuerySet API文档

在查询时发生了什么(When QuerySets are evaluated) QuerySet 可以被构造&#xff0c;过滤&#xff0c;切片&#xff0c;做为参数传递&#xff0c;这些行为都不会对数据库进行操作。只要你查询的时候才真正的操作数据库。 下面的 QuerySet 行为会导致执行查询的操作&#xff1a…

Spring自动扫描配置及使用方法

2019独角兽企业重金招聘Python工程师标准>>> 首先&#xff0c;检查一下你lib下有没有 common-annotations.jar 这个jar包 没有的话要导入工程。 下一步配置spring的配置文件applicationContex.xml&#xff0c;加入命名空间 红色为需要添加的内容 <beans xmlns…

Linux下ln命令使用

n是linux中又一个非常重要命令&#xff0c;它的功能是为某一个文件在另外一个位置建立一个同步的链接.当我们需要在不同的目录&#xff0c;用到相同的文件时&#xff0c;我们不需要在每一个需要的目录下都放一个必须相同的文件&#xff0c;我们只要在某个固定的目录&#xff0c…

6面向对象的程序设计

ECMA_262把对象定义为&#xff1a;无序属性的集合&#xff0c;其属性可以包含基本值、对象或者函数。 6.1理解对象 特性&#xff08;attribute&#xff09;是内部值&#xff0c;描述了属性&#xff08;property&#xff09;的各种特性。ECMAScript中有两种属性&#xff1a;数据…

DPM 2012 SP1---安装并部署DPM 2012 SP1服务器

实验拓扑图&#xff1a;一、前提条件&#xff1a;需要在DPM2012 SP1服务器上完成以下操作&#xff1a;1.DPM服务器加入域&#xff08;使用域管理员登陆DPM2012 SP1服务器&#xff09;2.准备存储磁盘&#xff08;新添加一块硬盘&#xff09;3.关闭防火墙服务&#xff08;DPM服务…

Linux下tail命令使用

linux tail命令用途是依照要求将指定的文件的最后部分输出到标准设备&#xff0c;通常是终端&#xff0c;通俗讲来&#xff0c;就是把某个档案文件的最后几行显示到终端上&#xff0c;假设该档案有更新&#xff0c;tail会自己主动刷新&#xff0c;确保你看到最新的档案内容。 一…

城市编号(省和市)

备注&#xff1a;这里只是个人的观点&#xff0c;有的地方也是copy&#xff0c;多多指教&#xff0c;个人笔记&#xff0c;有侵犯你们版权的地方还望海涵&#xff01;&#xff01;&#xff01; "qq": [ { "describe_content": "省", "type&…

Linux下test命令使用

test命令格式&#xff1a; [cpp] view plain copy test condition 通常&#xff0c;在if-then-else语句中&#xff0c;用[]代替&#xff0c;即[ condition ]。注意&#xff1a;方括号两边都要用空格。 1、数值比较 比 较 描 述 ---------------------------------------…