Struts的简单搭建(1)

Struts的简单搭建

本文主要讲了如何搭建一个简单的Struts工程,其目的只为了帮助大家更好的理解Struts2的执行流程.


Struts的相关基础知识

Struts2 是什么? 是一个扩展的用来建立企业级JAVA WEB应用程序的框架,不但注重程序的开发程序,更注重部署和后期维护. Struts2 使用OGNL可以访问值栈,OGNL对集合和索引属性的支持非常强大 Struts2核心控制器 在Struts2中,核心控制器是Filter,而不是一个不同的servlet,为了实现AOP概念 Struts框架的组成: 核心控制器FilterDispatcher(Struts2系统给的),业务控制器和用户事项的业务逻辑组件(用户自己提供) Struts 2框架获得了.action请求后,将根据.action请求的前面部分决定调用哪个业务逻辑组件,例如,对于login.action请求,Struts 2调用名为login的Action来处理该请求。 Struts 2应用中的Action都被定义在struts.xml文件中,在该文件中定义Action时,定义了该Action的name属性和 class属性,其中name属性决定了该Action处理哪个用户请求,而class属性决定了该Action的实现类。 数据流程图(执行流程) HTTP请求—–>核心控制器(web.xml)—Struts.xml—->Action—>IOC方法注值—->Action调用业务逻辑处理业务逻辑(3层)—>JSP()返回result)—>HTTP响应到客户端 1、客户端浏览器发出HTTP请求。 2、根据web.xml配置,该请求被FilterDispatcher接收。 3、根据struts.xml配置,找到需要调用的Action类和方法, 并通过IoC方式,将值注入给Aciton。 4、Action调用业务逻辑组件处理业务逻辑,这一步包含表单验证。 5、Action执行完毕,根据struts.xml中的配置找到对应的返回结果result,并跳转到相应页面。 6、返回HTTP响应到客户端浏览器



struts2

搭建

1 web.xml

[核心过滤器]

/* 过滤所有

<filter-class> StrutsPrepare

①纯手打
source

<?xml version="1.0" encoding="UTF-8"?>


<web-app xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)" xmlns="[http://xmlns.jcp.org/xml/ns/javaee](http://xmlns.jcp.org/xml/ns/javaee)" xsi:schemaLocation="[http://xmlns.jcp.org/xml/ns/javaee](http://xmlns.jcp.org/xml/ns/javaee)[http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd](http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd)" id="WebApp_ID" version="3.1">


  <display-name>Struts01</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>


  <!--  -->


    <filter>


        <filter-name>struts</filter-name>


        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>


    </filter>


    <filter-mapping>


        <filter-name>struts</filter-name>


        <url-pattern>/*</url-pattern>


    </filter-mapping>


</web-app>

②Design Filters

这里写图片描述

2.Action

package com.xxh.entity;


public class LoginAction {


private Integer password;


private String userName;


public String execute(){


      System.out.println("userName"+userName+"password"+password);


      return "success";


}


public Integer getPassword() {


      return password;


}


public void setPassword(Integer password) {


      this.password = password;


}


public String getUserName() {


      return userName;


}


public void setUserName(String userName) {


      this.userName = userName;


}


}

3.dtd 约束文件

struts.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](http://struts.apache.org/dtds/struts-2.3.dtd)">


<struts>


<!--注册包 包的名字可以随便取-->


<package name="xxx" namespace="/" extends="struts-default">


<!-- 注册类  name是前台需要传参的地址 class是Action的全类目,方便能找到Action-->


<action name="login" class="com.xxh.entity.LoginAction">


<result name="success">/success.jsp</result>


</action>


</package>


</struts>

容易出错的地方

HTTP Status 404 - There is no Action mapped for namespace [/] and action name [login] associated with context path

struts.xml 在必须在src下,或者在namespace下修改路径,默认的struts.xml在src下

4.在WebRoot下的WEB-INF中的index.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>


        </head>


  <body>


   <form action="login" method="post">


      用户名:<input type="text" name="username"><br>


      密&nbsp;码:<input type="password" name="password"><br>


      <input type="submit" value="登录">


    </form>


  </body>


</html>

创建一个登陆成功的界面

<%@ page language="java" import="java.util.*"


      pageEncoding="UTF-8"%>


<%


String path = request.getContextPath();


String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";


%>


<html>


  <head>


  </head>


  <body>


     欢迎您:${username}


  </body>


</html>

注意事项
1web.xml中过滤器的名称可以随便起
2LoginAction notfound
struts.xml中的全类名写错了
3LoginAction 中的execute(),他是struts的默认的执行方法
4.配置文件名称必须是struts.xml,全小写
报404错:There is no Action mapped
5如果修改tomact默认的shutdown端口,服务器不能正常启动
6.web.xml核心过滤器必须有

执行流程
index.jsp—>请求[先找核心过滤器(web.xml)]—->映射到struts.xml文件—->实体类[login,返回值success]—->execute方法返回success—->success.jsp页面[el输出]
前后台传参;
前台属性的值
后台:封装成对象中的属性进行接收

//代码流程:
1:导包
2:配置web.xml
3:新建实体类
4:新建struts.xml
5:index
6:输出 success

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值