1  创建一个web项目。

2 导入必要的JAR文件。

放在WEB-INF下lib包里。

3 添加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>StrutsDemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- struts2.1.3之后的版本,可以在该过滤器之前之间定义一定的过滤器-->
<!-- 定义struts2 的核心控制器,用于生成ActionMapper ,拦截所有的Action请求-->
<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> </web-app>

4  编写Action。

  1. Struts2直接使用Action来封装HTTP请求参数,因此Action类应该包含与请求相对应的属性,并为该属性提供对应的setter和getter方法。
  2. 为Action类里增加一个execute方法,因为Struts2框架默认会执行这个方法。这个方法本身并不做业务逻辑处理,而是调用其他业务逻辑组件完成这部分工作。
  3. Action类返回一个标准的字符串,该字符串是一个逻辑视图名,该视图名对应实际的物理视图。

我们来写个用户登录验证,提供用户名和密码两个属性。如果正确返回success否则返回error。

 package com.cy.action;

 import com.opensymphony.xwork2.ActionSupport;

 public class LoginAction extends ActionSupport {

     private static final long serialVersionUID = 1L;

     private String userName;
private String password; public String execute() { if (userName.equals("hellokitty") && password.equals("123")) { return SUCCESS;
} else {
return ERROR;
} } 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;
} }

Action有一下特点:

  • Struts2框架中Action是一个POJO,没有被代码污染。
  • Struts2中的Action的execute方法不依赖于servlet API,改善了Struts1中耦合过于紧密,极大方便了单元测试。
  • Struts2的Action无须用ActionForm封装请求参数。

5  添加框架核心配置文件struts.xml文件:在WEB-INF/classes文件夹下创建struts.xml。

在struts2-core-2.3.16.jar中有strust-defalut.xml.我们需要继承它。

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<package name="default" extends="struts-default">
<action name="login" class="com.cy.action.LoginAction">
<result name="success">/jsp/success.jsp</result>
<result name="error">/jsp/error.jsp</result>
</action>
</package> </struts>
  1. 在action标签中定义了name和class。name属性对应的是用户URL请求中的action名,class属性是处理请求的实现类(注意:要包含完整路径)。
  2. result标签定义逻辑视图和物理视图之间的映射,在我们的Action中,如果返回的字符串是"success”则由对应的success.jsp页面进行处理;如果返回的字符串是"error”则由error.jsp页面进行处理。

6 编写界面

6.1 login.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'Login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<form action="login" method="post">
用户名:<input type="text" name="userName"><br/>
密&nbsp;&nbsp;&nbsp;&nbsp;码:<input type="password" name="password"/><br/>
<input type="submit" value="提交"/>
<input type="reset" value="重置"/>
</form>
</body>
</html>

6.2 success.jsp

 <body>
<h1>登录成功!</h1>
</body>

6.3 error.jsp

 <body>
<h1>登录失败!</h1>
</body>

整体:

搭建一个简单的Struts2框架的更多相关文章

  1. 搭建一个简单的springMVC框架

    //新建一个简单的maven项目,选择war包 //web.xml配置 <?xml version="1.0" encoding="UTF-8"?> ...

  2. 搭建一个简单的mybatis框架

    一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...

  3. Flutter学习三之搭建一个简单的项目框架

    上一篇文章介绍了Dart的语法的基本使用,从这篇文章开始,开发一个基于玩Android网站的app.使用的他们开放的api来获取网站数据. 根据网站的结构,我们app最外层框架需要添加一个底部导航栏, ...

  4. 搭建一个简单的Struts2(Struts2_HelloWorld)

    1.导入Jar包 2.配置web.xml 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-ap ...

  5. 使用maven+eclipse搭建最简单的struts2的helloworld

    使用maven+eclipse搭建最简单的struts2的helloworld 一.web分层结构简介 1.web[细]粒度分层结构: 按细粒度分层可以分为以下6种: 1).表现层:html/css/ ...

  6. 用Python写一个简单的Web框架

    一.概述 二.从demo_app开始 三.WSGI中的application 四.区分URL 五.重构 1.正则匹配URL 2.DRY 3.抽象出框架 六.参考 一.概述 在Python中,WSGI( ...

  7. 如何创建一个简单的struts2程序

    如何创建一个简单的Struts2程序 “计应134(实验班) 凌豪” 1.创建一个新的Web项目test(File->new->Web Project) 2.Struts2框架的核心配置文 ...

  8. 【netty】(2)---搭建一个简单服务器

    netty(2)---搭建一个简单服务器 说明:本篇博客是基于学习慕课网有关视频教学.效果:当用户访问:localhost:8088 后 服务器返回 "hello netty"; ...

  9. Prism for WPF 搭建一个简单的模块化开发框架(三) 给TreeView加样式做成菜单

    原文:Prism for WPF 搭建一个简单的模块化开发框架(三) 给TreeView加样式做成菜单 昨天晚上把TreeView的样式做了一下,今天给TreeView绑了数据,实现了切换页面功能 上 ...

随机推荐

  1. UVa 213,World Finals 1991,信息解码

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  2. 为测试框架model类自动生成xml结果集

    问题:有大量类似于theProductId这样名字的字符串需要转换成the_product_id这种数据库column名的形式. 思路:见到(见)大写字母(缝)就插入(插)一个“_”字符(针)进去,最 ...

  3. nylg 640 Geometric Sum

    Geometric Sum 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 Compute (a + a^2 + … + a^n) mod m.(a+a2+…an)m ...

  4. 2016 ACM/ICPC Asia Regional Qingdao Online HDU5883

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5883 解法:先判断是不是欧拉路,然后枚举 #pragma comment(linker, "/S ...

  5. EasyUI DataGrid 添加排序

    这个事例演示了如何在点击列头的时候排序DataGrid中全部的列可以通过点击列头被排序.你可以定义可以被排序的列.默认的,列不能被排序除非你设置sortable属性为TRUE,下面是例子:标记 < ...

  6. 解决maven项目将model version改成3.0版本问题

    找到项目目录,找到.setting文件 找到org.eclipse.wst.common.project.facet.core.xml文件 修改如下标签 <installed facet=&qu ...

  7. map reduce filter

    三个函数比较类似,都是应用于序列的内置函数.常见的序列包括list.tuple.str.   1.map函数 map函数会根据提供的函数对指定序列做映射. map函数的定义: map(function ...

  8. webserer错误

    HTTP 错误 404.17 - Not Found 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理. 解决方法: C:\Program Files\Microsoft Visual Stu ...

  9. myeclipse页面编辑框空格、回车符、对齐出现特殊字符

    myeclipse页面编辑框空格.回车符.对齐出现特殊字符 解决办法:window-preferences-general-editors-Text Editors    把show whitespa ...

  10. 是否用new来新建对象

    class A{ }: 1.不使用new来新建对象 A a: 使用完后什么也不用做,系统自动调用析构函数.使用空间是栈. 2.使用new来新建对象 A* a=new A();   delete a;/ ...