如有转载,请申明:
转载至 http://blog.csdn.net/qq_35064774/article/details/64934709
前言
What ? 最近怎么开始写后端的博客了?
从去年开始就经常看到别人提“移动开发寒冬”,而年初投简历的时候更是亲身体会,不写3年经验连面试机会都没有,那么没有经验或经验少的人能怎么办呢,从一开就找不到工作怎么可能会有经验?
然而绝望并没有用,我算运气好,勉强找到一份工作。
从趋势来看,近几年移动开发待遇不会很好,今年发现到处缺后端,于是决定还是搞后端吧,因为搞安卓不过一年,后端的知识还没忘完(好吧,实际上以前后端也没多深入),于是利用下班时间复习后端(好吧,其实好多是预习了●﹏●),并把知识整理出来分享给大家。
目录
1.环境搭建
导包
直接下载导入
http://struts.apache.org/download.cgi
可根据需要选择 all min src 等包,如果仅仅是学习,选择 min 包就够了版本管理工具导入
- Gradle
compile "org.apache.struts:struts2-core:2.5.10.1"
- Maven
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.10.1</version>
</dependency>
上面的仅仅是核心包,根据需要可引入其他包,比如注解包
compile "org.apache.struts:struts2-convention-plugin:2.5.10"
- Gradle
配置
web.xml
struts2 基于拦截器,因此,配置的第一步是在 WEB-INF/web.xml 中配置 struts2 拦截器。
配置的时候需要注意,不同的版本的 StrutsPrepareAndExecuteFilter
包路径不一样,配置的时候,可以用 IED 搜索这个类,复制路径。比如 Idea 的搜索类快捷键是 Ctrl + N
。
* 2.5.x 版本
```
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
```
为了防止篇幅过大,其他版本就不写了。
struts.xml
在 src 目录下建立 struts.xml,Idea 中需要放到 resources 下。
<?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>
</struts>
2.Action
HelloAction
类
编写一个类继承 ActionSupport, 并提供一个无参返回字符串且公开的方法,如下:
public class HelloAction extends ActionSupport {
public String hello() throws Exception {
System.out.println("hello world!");
return SUCCESS;
}
}
配置 Action
类似于 Servlet,struts2 使用 action 来处理请求。
使用 action 需要进行配置。
xml 配置
在 struts.xml 的 struts 标签里面配置 action
如下:
<package name="p1" extends="struts-default">
<action name="hello" class="com.ittianyu.web.action.HelloAction" method="login">
<result name="success">hello.jsp</result>
</action>
</package>
action 外面是 package,类似于 java 的包。
这里简单介绍 action 的属性,后面配置详解里会一一介绍大部分标签和属性。
name: 访问 action 的名称,如果没有配置 namespace,则 根路径/name 就是 action 的访问地址。
class 和 method 则是指定 action 的类和方法。
注解配置
使用注解前必须保证 已经导入了 struts2-convention-plugin 的包。
在 Action 类上加上 @ParentPackage("struts-default")
在对应的方法上加上
@Action(value = "hello", results = {
@Result(name = "success", location="/hello.jsp")})
如下:
@ParentPackage("struts-default")
public class HelloAction extends ActionSupport {
@Action(value = "hello", results = {
@Result(name = "success", location="/hello.jsp")})
public String hello() throws Exception {
System.out.println("hello world!");
return SUCCESS;
}
}
配置好后,就可以通过浏览器访问(根路径取决于项目配置)
http://127.0.0.1/strut2/hello.action
如果访问成功,控制台后打印 hello world!
。
向 Action 传递参数
登录是很常见的 action,这个时候一般要向服务器传递 username, password 等。
User
创建一个 User 实体对象,假设里面只有 username, password。
public class User {
private String username;
private 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;
}
}
UserAction
创建一个 UserAction 来处理请求,简单起见,使用注解方式配置。
@ParentPackage("struts-default")
public class UserAction extends ActionSupport implements ModelDriven<User>{
private User user = new User();
@Action(value = "login", results = {
@Result(name = "success", location="/home.jsp")})
public String login() throws Exception {
System.out.println(user.getUsername());
System.out.println(user.getPassword());
return SUCCESS;
}
@Override
public User getModel() {
return user;
}
}
实现 ModelDriven 方法,返回 user。
然后在执行 login 方法之前,ModelDriven 拦截器会给 user 设置请求提交的值。
获取 Servlet Api
使用 ServletActionContext
比如:
ServletActionContext.getRequest();
ServletActionContext.getResponse();
3.配置详解
include
用于引入其他 struts2 配置文件
比如:
<struts>
<include file="struts-part1.xml"/>
<include file="struts-part2.xml"/>
</struts>
package
名称 | 类型 | 默认 | 必须 | 描述 |
---|---|---|---|---|
name | string | 无 | yes | 包的名称 |
extends | string | 无 | no | 这个包所继承的父包名称.一般继承 struts-default |
namespace | string | / | no | 包的命名空间,如果配置了,在访问时要加上包名 |
abstract | boolean | false | no | 是否是抽象包,如果是,专门设计来被继承,一般配置了自己的拦截器栈时,会设计基础包。 |
action
名称 | 类型 | 默认 | 必须 | 描述 |
---|---|---|---|---|
name | string | 无 | yes | 动作的名称 |
class | string | com.opensymphony.xwork2.ActionSupport | no | 绑定的动作类。若不配置,则为默认返回 success 的动作类。 |
method | string | execute | no | 动作绑定的方法 |
converter | string | 无 | no | 动作的类型转换器 |