Struts2

一、Struts2

Struts2技术是用于界面和程序交互的一种框架,可提高MVC中V和C的交互效率。

Struts2是 Apache 软件组织推出的一个基于 MVC 模式的轻量级 Web 框架,自问世以来,就受到了广大 Web 开发者的欢迎。目前,Struts2 在 Java Web 开发领域中已占据了十分重要的地位。

目前为止,Struts 框架拥有两个主要版本:Struts1.x 和 Struts2.x。Struts1 是最早的基于 MVC 模式的轻量级 Web 框架,它能够合理划分代码结构,并包含验证框架、国际化框架等多种实用工具框架。

随着技术的不断进步,Struts1 的局限性也越来越多地暴露出来。为了符合更加灵活、高效的开发需求,Struts2 框架应运而生,并在逐渐取代 Struts1 框架。

Struts2 是在 Struts1 和 WebWork 技术(WebWork 是由 OpenSymphony 组织开发的、致力于组件化和代码重用的 J2EE Web 框架,它也是一个 MVC 框架)的基础上进行合并后的全新框架。

虽然 Struts2 的名字与 Struts1 相似,但其设计思想有很大不同,因为 Struts2 是以 WebWork 为核心的,它是 WebWork 技术与 Struts1 技术的结合,所以 Struts2 可以理解为 WebWork 的更新产品。



二、Tomcat

将 Tomcat 作为服务依靠

Apache 为HTML页面服务,Tomcat 运行JSP 页面和Servlet。

Tomcat和IIS等Web服务器一样,具有处理HTML页面的功能,

它还是一个Servlet和JSP容器,独立的Servlet容器是Tomcat的默认模式。

Tomcat安装教程

Tomcat| Tomcat的目录结构详细介绍(超全)



三、前端界面(MVC——V)

新建一个 Java Web 项目

仅用 html 实现页面布局

首页

创建 index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

  <title>My JSP 'index.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>

<table border=1 width=800 align=center>
  <tr><td><img src=Hydrangeas.jpg width=800 height=100></td></tr>
  <tr>
    <td align=center>
      <table width=400>
        <tr><td><a href=login.jsp>登录</a></td><td><a href=register.jsp>注册</a></td><td>新闻</td><td>下载</td></tr>
      </table>
    </td>
  </tr>
  <tr>
    <td>
      <font size=5 color=red><center> 寒假放假通知</center></font>
      <br><br>
      寒假放假通知寒假放假通知,寒假放假通知,寒假放假通知,寒假放假通知,寒假放假通知寒假放假通知,寒假放假通知,寒假放假通知寒假放假通知寒假放假通知
      寒假放假通知寒假放假通知,寒假放假通知,寒假放假通知,寒假放假通知,寒假放假通知寒假放假通知,寒假放假通知,寒假放假通知寒假放假通知寒假放假通知
      <br><br>
      寒假放假通知寒假放假通知,寒假放假通知,寒假放假通知,寒假放假通知,寒假放假通知寒假放假通知,寒假放假通知,寒假放假通知寒假放假通知寒假放假通知
      寒假放假通知寒假放假通知,寒假放假通知,寒假放假通知,寒假放假通知,寒假放假通知寒假放假通知,寒假放假通知,寒假放假通知寒假放假通知寒假放假通知
      <br><br>
      <center><img src=Hydrangeas.jpg width=400 height=300></center>
    </td>
  </tr>

  <tr><td height=100 bgcolor=green></td></tr>
</table>

</body>
</html>

注册界面

创建 register.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 'MyJsp.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=a.action method=post>

    <table align=center border=1 width=500>

        <tr><td>用户名</td><td><input type=text name=username></td></tr>

        <tr><td>密码</td><td><input type=text name=userpassword></td></tr>

        <tr><td>邮箱</td><td><input type=text name=email></td></tr>

        <tr><td>班级</td><td><input type=text name=userclass></td></tr>

        <tr><td>地址</td><td><input type=text name=address></td></tr>

    </table>

    <p align=center><input type=submit value=注册><input type=reset value=重写></p>

</form>
    <center><s:property value="showinfo"/></center> 

</body>
</html>

登录界面

创建 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 'index.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=b.action method=post>
      <table width=400 align=center border=1>
          <tr><td>用户名:</td><td> <input type=text name=username>   </td></tr>
          <tr><td>密码:</td><td> <input type=text name=userpassword> </td></tr>
          <tr><td colspan=2 align=center><input type=submit value=登录></td></tr>
      </table>
      <!--
	  当action返回到指定页面时,可以通过该标签显示action中的属性信息。(注 userName必须在action中存在,并有  	  GET方法)。
 	  -->
      <center><s:property value="showinfo"/></center>
  </form>
    
</body>
</html>

管理界面

创建 manage.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
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 'index.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>

  <frameset rows="25%,75%">
      <frame src="top.jsp">
      <frameset cols="20%,80%">
          <frame src="left.jsp">
          <frame src="main.jsp" name="main">
      </frameset>
  </frameset>

</html>

管理界面——分支模块1

创建 top.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
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 'index.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>

     top

  </body>
</html>

管理界面——分支模块2

创建 left.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
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 'index.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>
     新闻分类管理<br/>
     <a href="addnewclass.jsp" target="main"> --新闻分类添加</a> <br/>
     --新闻分类构思<br/>
  </body>
</html>

管理界面——分支模块3

创建 main.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
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 'index.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>

     main

  </body>
</html>

管理界面——链接文件1

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
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 'index.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>
    <input type="text" name="newsclassname"><input type="submit" value="添加">
  </body>

</html>


四、配置struts2

方式一:

  1. 新建项目时直接添加:
    请添加图片描述

  2. Struts2 的官网 http://struts.apache.org 下载struts2核心文件

    配置:

    1.将struts2六个核心类(扩展名为Jar文件)包放入web\WEB-INF\lib
    请添加图片描述

    2.将web\WEB-INF\web.xml替换为新的web.xml

    3.将struts.xml文件复制到src目录下

    4.运行一下web工程,只要能正常运行,struts2配置成功



五、处理程序(MVC——C)

业务处理程序就是java程序

被用来处理从前端页面传来的数据

src目录下创建Reg包

Reg包下创建一个RegDeal类(当前仅测验struts技术,后续将更新代码,使用Hibernate技术对数据库进行操作):
请添加图片描述

package Reg;

public class RegDeal {
    private String username;
    private String userpassword;
    private String email;
    private String userclass;
    private String address;
    private String showinfo;

    // 写操作数据库的程序
    public String todatabase() {
        // 后续使用Hibernate技术对数据库进行操作,此处仅在当前作提示
        System.out.println("此处是属性写入数据库的代码");
        System.out.println(username);
        System.out.println(userpassword);
        System.out.println(email);
        System.out.println(userclass);
        System.out.println(address);
        if ("tom".equals(username)) {
            showinfo = username + "已经注册过!";
            return "error";
        } else if ("".equals(username) || "".equals(userpassword)) {
            showinfo = "用户名或密码不能为空!";
            return "error";
        } else {
            return "success";
        }

    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUserpassword() {
        return userpassword;
    }

    public void setUserpassword(String userpassword) {
        this.userpassword = userpassword;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getUserclass() {
        return userclass;
    }

    public void setUserclass(String userclass) {
        this.userclass = userclass;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getShowinfo() {
        return showinfo;
    }

    public void setShowinfo(String showinfo) {
        this.showinfo = showinfo;
    }

}

更改 struts.xml 配置文件:

接受页面数据将其转换为java中的对象

进而在java程序中对对象进行业务处理

<?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="struts2"  extends="struts-default">
                
    <!-- 
     <action name="SelectStudentbyidAction" class="Studentaction.SelectStudentbyidAction">
      <result name="success">studentupdate.jsp</result>
      </action>
      
      <action name="UpdatestudentAction" class="Studentaction.UpdatestudentAction">
      <result name="success" type="redirect-action">ShowstudentAction.action</result>
      </action> 
    -->
       
        <!-- 
    此处为更改内容1,action行为的作用是连接前端页面和java程序,register.jsp界面中用户注册信息提交时,通过<form action=a.action method=post>创建一个a对象,xml文件接收到此对象,用a对象调用Reg.RegDeal这段java代码中的todatabase方法。
    此时java程序中包含逻辑判断,如果判断返回success,跳转到login.jsp界面,判断返回error,跳转到reg.jsp界面。
		-->
       <action name="a" class="Reg.RegDeal" method="todatabase">
           <result name="success">login.jsp</result>
           <result name="error">reg.jsp</result>
       </action>
         
       <!-- 
    此处为更改内容2,action行为的作用是连接前端页面和java程序,log.jsp界面中用户输入登录信息时,通过<form action=b.action method=post>创建一个b对象,xml文件接收到此对象,用b对象调用Log.LoginDeal这段java代码中的login方法。
    此时java程序中包含逻辑判断,如果判断返回success,跳转到manage.jsp界面,判断返回error,跳转到login.jsp界面。
	  -->
      <action name="b" class="Log.LoginDeal" method="login">
      	   <result name="success">manage.jsp</result>
           <result name="error">login.jsp</result>
      </action>

    </package>
</struts>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值