Jimoshi成长经历:前面的笔记后面再慢慢整理-------方便自己
目录:什么是Struts2、通过maven 创建struts2 的项目、处理接收值
Struts2 框架学习(一):
一、什么是Struts2?
1、Struts2的概念:Struts2是一个基于MVC设计模式的Web应用框架,Struts 2以WebWork为核心,采用拦截器的机制来处理用户的请求。Struts 2的Action是一个请求对应一个实例(每次请求时都新new出一个对象),没有线程安全方面的问题,Struts2整合了OGNL(Object Graph NavigationLanguage),Struts2使用“ValueStack”技术。
2、拦截器:在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作。拦截是AOP的一种实现策略。
注:拦截器,在struts2-core.jar这个包下的struts-default.xml中
二、通过maven 创建struts2 的项目
1、添加jar包依赖(在pom.xml中)
代码示例:
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.16.3</version>
</dependency>
2、配置web.xml,添加过滤器,让所有的请求都经过struts2处理
代码示例:
<!-- 添加过滤器,让所有请求都经过struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3、添加struts.xml文件交由zhj.xml文件配置(在main/resources中添加)
代码示例:
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>
<include file="zhj.xml"></include>
</struts>
//团队任务的时候都是自己配置自己的xml文件,以便管理(协作开发)
zhj.xml文件,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//ApacheSoftware Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 后面配置的请求内容填入该位置-->
</struts>
4、在(com.zr.controller中添加LoginAction类)添加对应的请求的类继承ActionSupport类 (也可以使用实现Action接口)重写excute的方法
代码示例:
//public class LoginAction extends ActionSupport { }
public class LoginAction extends ActionSupport{
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
}
5、再在zhj.xml中配置相应的请求
代码示例:
<struts>
<!-- 设置开发模式:修改XML文件后无需在次重启,能够显示更多的信息;缺点 运行效率降低 -->
<constant name="struts.devMode" value="true" />
<!-- 热部署 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<package name="zr" extends="struts-default">
<action name="helloworld" class="com.zr.controller.LoginAction">
<result name="success">index.jsp</result>
</action>
</package>
</struts>
6、测试(在前面建立好对应的index.jsp页面<body>你好,欢迎你</body>)
步骤:发送helloworld请求-->到对应的xml中找匹配的action-->交给对应的action类来处理 ->重写了action的excute方法-->返回对应的result-->在找struts.xml中匹配的result来处理-->index.jsp
三、处理接收值
描述:直接在action里面定义属性,实现get和set的方法。属性的类型也可以直接定义,自动帮我们进行转换。set在做请求,struts2 是在调用set方法的时候把参数设置,get在返回的时候struts2调用get的方法将值获取,值可以通过EL表达式直接取得。当然在struts.xml中,同样可以使用EL表达式
1、传入javabean对象(属性驱动)
1.保证对象的模型
代码示例:
public class Person {
private String pname;
private int page;
(进行get,set)
}
2.在LoginAction中定义接受的对象
代码示例:
private Person person;
(进行get,set)
3.前台做请求的时候通过对象名.对象的属性名传递
代码示例:
<form action="helloworld" method="post">
<input type="text" name="person.pname"/>
<input type="text" name="person.page"/>
<input type="submit"/ value="提交">
</form>
2、传入javabean对象(模型驱动)
1.保证对象模型的存在
2.在LoginAction中实现ModelDriven<Person>接口并且重写其中方法
代码示例:
public class LoginAction implements ModelDriven<Person> {
@Override
public Person getModel() {
// TODO Auto-generated method stub
return xy;
}
}
3.前台传值的时候直接写对象属性名
代码示例:
<form action="helloworld" method="post">
<input type="text" name="pname"/>
<input type="text" name="page"/>
<input type="submit"/ value="提交">
</form>
3、在传递相同name值(在LoginAction类中,定义数组接受)
1.编写前台index.jsp页面
代码示例:
<form action="helloworld" method="post">
<input type="text" name="uname"/>
<input type="text" name="uname"/>
<input type="submit"/ value="提交">
</form>
2.在LoginAction中实现Action接口并且重写其中方法
代码示例:
public class LoginAction implements Action{
private String[] uname;
(进行get,set方法)
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
for (int i = 0; i < uname.length; i++) {
System.out.println(uname[i]);
}
return "abc";
}
}
3.配置zhj.xml文件
代码示例:
<action name="helloworld" class="com.zr.controller.LoginAction">
<result name="abc">index.jsp</result>
</action>
4、集合接收
1.编写前台index.jsp页面
代码示例:
<form action="helloworld" method="post">
<table>
<tr>
<td><input type="text" name="persons[0].pname"/></td>
<td><input type="text" name="persons[0].page"/></td>
</tr>
<tr>
<td><input type="text" name="persons[1].pname"/></td>
<td><input type="text" name="persons[1].page"/></td>
</tr>
</table>
<input type="submit" value="提交"/>
</form>
2.在LoginAction中实现Action接口并且重写其中方法
代码示例:
public class LoginAction implements Action{
private List<Person> persons;
(进行get,set)
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
for (Person person : persons) {
System.out.println(person.getPname());
System.out.println(person.getPage());
}
return "abc";
}
}
5、zhj.xml文件中的package和action
package: package(分包管理)
name(名字要见名知意)
namespace(请求空间,相当在请求上面多加一个请求的地址)
extends(继承某个文件)
action: name(请求路径)
class(交由action 类来处理)
method(表明该请求由哪一个方法来进行处理)
代码示例:
zhj.xml配置文件中:
<package name="zr" namespace="/" extends="struts-default">
<action name="findStus" class="com.zr.controller.LoginAction" method="findStus">
<result name="abc">index.jsp</result>
</action>
</package>
LoginAction类中:
public String findStus(){
System.out.println("进入到该方法了");
return "abc";
}
6、关于action中方法的调用(指定method属性)
1.通配符方式“*”
代码示例:
<action name="*_*" class="com.zr.controller.{1}Action" method="{2}">
<result name="abc">index.jsp</result>
</action>
请求方式:http://localhost:8080/struts2/Login_findStus
2.通过使用“!”的方式进行动态调用(action中就不需要配置method属性)(不建议使用)
代码示例:
在struts.xml中配置
<!-- 开启动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
使用感叹号的方式进行请求:http://localhost:8080/struts2/student!findStus
目录:什么是Struts2、通过maven 创建struts2 的项目、处理接收值
Struts2 框架学习(一):
一、什么是Struts2?
1、Struts2的概念:Struts2是一个基于MVC设计模式的Web应用框架,Struts 2以WebWork为核心,采用拦截器的机制来处理用户的请求。Struts 2的Action是一个请求对应一个实例(每次请求时都新new出一个对象),没有线程安全方面的问题,Struts2整合了OGNL(Object Graph NavigationLanguage),Struts2使用“ValueStack”技术。
2、拦截器:在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作。拦截是AOP的一种实现策略。
注:拦截器,在struts2-core.jar这个包下的struts-default.xml中
二、通过maven 创建struts2 的项目
1、添加jar包依赖(在pom.xml中)
代码示例:
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.16.3</version>
</dependency>
2、配置web.xml,添加过滤器,让所有的请求都经过struts2处理
代码示例:
<!-- 添加过滤器,让所有请求都经过struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3、添加struts.xml文件交由zhj.xml文件配置(在main/resources中添加)
代码示例:
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>
<include file="zhj.xml"></include>
</struts>
//团队任务的时候都是自己配置自己的xml文件,以便管理(协作开发)
zhj.xml文件,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//ApacheSoftware Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 后面配置的请求内容填入该位置-->
</struts>
4、在(com.zr.controller中添加LoginAction类)添加对应的请求的类继承ActionSupport类 (也可以使用实现Action接口)重写excute的方法
代码示例:
//public class LoginAction extends ActionSupport { }
public class LoginAction extends ActionSupport{
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
}
5、再在zhj.xml中配置相应的请求
代码示例:
<struts>
<!-- 设置开发模式:修改XML文件后无需在次重启,能够显示更多的信息;缺点 运行效率降低 -->
<constant name="struts.devMode" value="true" />
<!-- 热部署 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<package name="zr" extends="struts-default">
<action name="helloworld" class="com.zr.controller.LoginAction">
<result name="success">index.jsp</result>
</action>
</package>
</struts>
6、测试(在前面建立好对应的index.jsp页面<body>你好,欢迎你</body>)
步骤:发送helloworld请求-->到对应的xml中找匹配的action-->交给对应的action类来处理 ->重写了action的excute方法-->返回对应的result-->在找struts.xml中匹配的result来处理-->index.jsp
三、处理接收值
描述:直接在action里面定义属性,实现get和set的方法。属性的类型也可以直接定义,自动帮我们进行转换。set在做请求,struts2 是在调用set方法的时候把参数设置,get在返回的时候struts2调用get的方法将值获取,值可以通过EL表达式直接取得。当然在struts.xml中,同样可以使用EL表达式
1、传入javabean对象(属性驱动)
1.保证对象的模型
代码示例:
public class Person {
private String pname;
private int page;
(进行get,set)
}
2.在LoginAction中定义接受的对象
代码示例:
private Person person;
(进行get,set)
3.前台做请求的时候通过对象名.对象的属性名传递
代码示例:
<form action="helloworld" method="post">
<input type="text" name="person.pname"/>
<input type="text" name="person.page"/>
<input type="submit"/ value="提交">
</form>
2、传入javabean对象(模型驱动)
1.保证对象模型的存在
2.在LoginAction中实现ModelDriven<Person>接口并且重写其中方法
代码示例:
public class LoginAction implements ModelDriven<Person> {
@Override
public Person getModel() {
// TODO Auto-generated method stub
return xy;
}
}
3.前台传值的时候直接写对象属性名
代码示例:
<form action="helloworld" method="post">
<input type="text" name="pname"/>
<input type="text" name="page"/>
<input type="submit"/ value="提交">
</form>
3、在传递相同name值(在LoginAction类中,定义数组接受)
1.编写前台index.jsp页面
代码示例:
<form action="helloworld" method="post">
<input type="text" name="uname"/>
<input type="text" name="uname"/>
<input type="submit"/ value="提交">
</form>
2.在LoginAction中实现Action接口并且重写其中方法
代码示例:
public class LoginAction implements Action{
private String[] uname;
(进行get,set方法)
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
for (int i = 0; i < uname.length; i++) {
System.out.println(uname[i]);
}
return "abc";
}
}
3.配置zhj.xml文件
代码示例:
<action name="helloworld" class="com.zr.controller.LoginAction">
<result name="abc">index.jsp</result>
</action>
4、集合接收
1.编写前台index.jsp页面
代码示例:
<form action="helloworld" method="post">
<table>
<tr>
<td><input type="text" name="persons[0].pname"/></td>
<td><input type="text" name="persons[0].page"/></td>
</tr>
<tr>
<td><input type="text" name="persons[1].pname"/></td>
<td><input type="text" name="persons[1].page"/></td>
</tr>
</table>
<input type="submit" value="提交"/>
</form>
2.在LoginAction中实现Action接口并且重写其中方法
代码示例:
public class LoginAction implements Action{
private List<Person> persons;
(进行get,set)
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
for (Person person : persons) {
System.out.println(person.getPname());
System.out.println(person.getPage());
}
return "abc";
}
}
5、zhj.xml文件中的package和action
package: package(分包管理)
name(名字要见名知意)
namespace(请求空间,相当在请求上面多加一个请求的地址)
extends(继承某个文件)
action: name(请求路径)
class(交由action 类来处理)
method(表明该请求由哪一个方法来进行处理)
代码示例:
zhj.xml配置文件中:
<package name="zr" namespace="/" extends="struts-default">
<action name="findStus" class="com.zr.controller.LoginAction" method="findStus">
<result name="abc">index.jsp</result>
</action>
</package>
LoginAction类中:
public String findStus(){
System.out.println("进入到该方法了");
return "abc";
}
6、关于action中方法的调用(指定method属性)
1.通配符方式“*”
代码示例:
<action name="*_*" class="com.zr.controller.{1}Action" method="{2}">
<result name="abc">index.jsp</result>
</action>
请求方式:http://localhost:8080/struts2/Login_findStus
2.通过使用“!”的方式进行动态调用(action中就不需要配置method属性)(不建议使用)
代码示例:
在struts.xml中配置
<!-- 开启动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
使用感叹号的方式进行请求:http://localhost:8080/struts2/student!findStus