今天翻看自己的博客,突然发现没有关于SSH三大框架的博客,翻看之前的笔记,有整理但是没发布 - -…
先从Struts2说起,使用一些新的技术,我的习惯是先以最快的速度学会如何使用它,做出demo,然后在去慢慢研究他的原理,实现机制。所以第一篇博客我会讲解如何使用Struts2框架快速搭建出一个项目。
需要注意:Struts2需要运行在JRE1.5及以上版本
1、创建一个Java Web项目
2、引入Struts2的依赖包,将依赖包拷贝到WEB-INF/lib下
Struts2 下载地址:http://struts.apache.org/
依赖的jar包如下:
* commons-logging-1.0.4.jar
* freemarker-2.3.15.jar
* ognl-2.7.3.jar
* struts2-core-2.1.8.1.jar
* xwork-core-2.1.6.jar
* commons-fileupload-1.2.1.jar
3、在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter,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>
4、提供struts2配置文件struts.xml,放到src下。
可以去struts-2.1.8.1\apps\WEB-INF\src\java目录下拷贝一份struts.xml作为模版。
5、建立JSP页面(login.jsp、login_success.jsp、login_error.jsp)
Login.jsp:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<form action="login.action">
用户:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
login_success.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
${username },登录成功
</body>
</html>
login_error.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
登录失败
</body>
</html>
6、创建Struts2的Action
Struts2的action可以不用继承Struts2框架中的任何类, 也不用实现Struts2框架中的任何接口,所以Struts2的Action可以是一个POJO(纯粹的Java对象), 所以Struts2的Action测试更容易
Struts2缺省方法名称:public String execute() throws Exception; 同时在Action中需要提供属性的getter和setter方法,便于收集数据(这样收集数据的模式一般可以成为属性驱动模式)
LoginAction 代码:
package com.bjpowernode.struts2;
public class LoginAction {
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;
}
public String execute() throws Exception {
if ("admin".equals(username) && "admin".equals(password)) {
return "success";
}else {
return "error";
}
}
}
7、将JSP和Action配置到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>
<!-- 需要继承struts-default包,这样就拥有的最基本的功能 -->
<package name="struts2" extends="struts-default">
<action name="login" class="com.bjpowernode.struts2.LoginAction">
<result name="success">/login_success.jsp</result>
<result name="error">/login_error.jsp</result>
</action>
</package>
</struts>
8、启动Tomcat测试
在浏览器输入:http//localhost:8080/xxx/login.action,进行测试
小结
至此,使用Struts2快速创建一个项目,已经完成。以后在开发只需要写对应的action和jsp代码,然后将其配置到struts.xml中即可。
Struts2其他的核心知识,后续博客中介绍。