struts2 入门案例、struts2-helloword

java 开发人员必须使用的网站
apache.org spring.org github.com 拨云剑 这些都是我们未来下载jar包的网站。
.
搭建struts2环境
1、struts2下载的文件中有已经现成写好的demo,我们可以参照这个例子来搭建struts2的架构:

这里写图片描述
2、导入jar包:导入lib下面的所有jar包,
这里写图片描述

3、配置web.xml
sturts2 是利用过滤器来进行实现,以后所有的请求都交给struts2的一个总过滤器,由他去进行请求分发,找到对应的javaBean来接收和执行请求,

<?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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>TestStruts2</display-name>

  <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>*.action</url-pattern>
       <!-- *.do -->
   </filter-mapping>



  <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>
</web-app>

4、配置本身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>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />


</struts>

编写helloword
编写一个jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
   <%
    String path = request.getContextPath() + "/";
   %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

    <a href="<%=path%>/user1/login.action">struts2 - Helloword</a>

</body>
</html>

编写一个普通的action

package com.xingxue.action;

public class UserAction {

    public String userLogin() {
        //获取参数 -- sruts2自己做


        //跳转页面 -- 通过配置文件帮我们跳转

        System.out.println("login");
        return "OK";
    }

}

配置struts.xml 实现利用struts2 完成控制器的工作

<?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>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <!--user表示包的唯一名字  --必须 
        /user1表示对外公布的url名字 --必须
        extends --表示继承自struts默认的xml文件  -- 必须
     -->
    <package name="user" namespace="/user1" extends="struts-default" >
    <!--login对外的url名字  --必须 
        class :对应的javaaction类
        method:调用那个方法 -- 必须
     -->
        <action name="login" class="com.xingxue.action.UserAction" method="userLogin">
            <result name="OK">/OK.jsp</result>
            <result name="NO">/NO.jsp</result>
        </action>
    </package>
</struts>

总结:
struts2是一个mvc框架,是利用filter,原理是通过在webxml配置一个struts2总控制器,拦截所有的请求,struts2在通过本身配置文件进行对应请求的分发,找到对应的javaBean的方法,执行该方法通过返回支付传决定跳转那个页面。

猜你喜欢

转载自blog.csdn.net/sky274548769/article/details/81163885
今日推荐