Struts2基础(一个简单的小例子)

来源:互联网 发布:淘宝联盟app推广教程 编辑:程序博客网 时间:2024/06/04 19:44

MVC(Model View Control):

模型:对数据库的操作

视图:用户与程序交互的界面

控制器:接收用户的输入并且调用模型可用户视图来完成用户需求

MVC优点:

1 低耦合

2 高重用性和可适用性

3 较低的生命周期成本

4 快速的部署

5 可维护性好

6 有利于软件工程化管理

Struts2工作流程:

1 客户端提交一个(HttpServletRequest)请求

2 请求被提交到一系列的过滤器(Filter)。过滤器顺序ActionContext CleanUp->其他过滤器->FilterDispatcher

3 FilterDispatcher接收请求,询问ActionMapper是否要调用某个Action来处理这个(HttpServetRequest),如果ActionMapper决定调用某个Action,FilterDispatcher把请求交给ActionProxy

4 ActionProxy通过Configuration Manager(struts.xml)询问框架的配置文件,找到调用的Action类

5 ActionProxy创建一个ActionInvocation实例,同时ActionInvocation通过代理模式调用Action。但在调用之前,ActionInvocation会根据配置加载Action相关的所有Interceptor(拦截器)

6 一旦Action执行完毕,ActionInvocation负责根据struts.xml中的配置找到对应的返回结果result。然后根据返回的结果呈现给客户。

Struts2下载地址:

http://struts.apache.org/

Struts2文件夹结构:

apps:存放Struts2的实例应用

docs:存放Struts2的相关文档

lib:存放Struts2框架的核心类库,以及Struts2的第三方插件

src:存放Struts2框架的全部源代码

创建项目:

File->New->Project...

MyEclipse->Java Enterprise Project->Web Project->Next

输入ProjectName->Finish

Struts2类库:

struts2-core-xxx.jar;(Struts2的核心类库)

xwork-xxx.jar;(xwork项目,Struts2就是在此基础上构建的)

common-logging-xxx.jar;(用于插入其他的日志系统)

freemarker-xxx.jar;(所有的Struts 2的UI标签模板)

ognl-xxx.jar(OGNL表达式语言)

把类库添加到项目中:

拷贝所有的jar文件到web-inf/lib文件夹下

配置web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
  <display-name>MyProject</display-name>
  <!-- 设置项目运行时的欢迎界面 -->
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern><!-- 表示所有的请求都有它处理 -->
  </filter-mapping>
</web-app>

创建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" extends="struts-default">
<action name="login" class="org.action.LoginAction">
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>

编写LoginAction.java:

package org.action;
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(){
return "success";//不做任何判断直接返回"success"
}
}

编写login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登陆界面</title>
</head>
<body>
<form action="login.action" method="post">
<table align="center" border=1 width="250" bgcolor="#F5FFE1">
<tr>
<td colspan="2" align="center" width="250" bgcolor="#CCCCFF">用户登录界面</td>
</tr>
<tr>
<td width="50">姓名:<input type="text" name="username" /></td>
</tr>
<tr>
<td width="50">密码:<input type="password" name="password"
size=22 /></td>
</tr>
<tr>
<td align="center" colspan=2><input type="submit" value="登陆" />
<input type="reset" value="重置" /></td>
</tr>
</table>
</form>
</body>
</html>

编写welcome.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>  
    <title>欢迎界面</title>
  </head>
  <body bgcolor=#f5ffe1>
  欢迎您!${username}
  </body>
</html>

部署运行:

Project->Add->tomcat 7.0->finish

浏览器输入:

http://localhost:8080/YourProjectName/login.jsp
























0 0
原创粉丝点击