• struts1.x mvc简单例程


    因为项目需要,需要使用struts1.x 。因此学习下struts框架。
    例程从struts 实现MVC框架入手,完成一次request请求。
    一.前台
    两个jsp文件
    index.jsp:
    只有一个button按钮,发送request请求

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="utf-8"%>
    <!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=utf-8">
    <title>Index</title>
    </head>
    <body>
        <form method="post" action="/structsLearning/roller-ui/createWebsite.do?method=create">
            <input type="submit" value="start website"/>
        </form>
    </body>
    </html>

    createWebsite.jsp:
    request请求成功页面显示,从后台传入一个值读取

    <h1><%=request.getAttribute("userName")%> is creating a website</h1> 

    二.配置文件
    web.xml:
    将所有的*.do请求都配置到struts-config.xml

     <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <init-param>  
            <param-name>debug</param-name>  
            <param-value>3</param-value>  
        </init-param>  
        <init-param>  
            <param-name>detail</param-name>  
            <param-value>3</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    struts-config.xml:
    parameter 指定method这样一个servlet 文件就可以对应多个不同的url请求。使用
    method=**即可。
    type属性指定了servlet class。
    path:指定了forward 的jsp文件

    <action-mappings>
        <action 
            path="/roller-ui/createWebsite"
            type="org.apache.roller.struts.CreateWebsiteAction"
            scope="request"
            parameter="method"
            >
        <forward
            name="createWebsite.page"
            path="/jsp/createWebsite.jsp"
        />
        </action>
    </action-mappings>

    三.java 文件
    CreateWebsiteAction.java:
    mapping.findForward(“createWebsite.page”)这边forward名字与struts-config.xml进行了映射。

    package org.apache.roller.struts;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.actions.DispatchAction;
    public class CreateWebsiteAction extends DispatchAction {
    
    public ActionForward create(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request,HttpServletResponse response) throws Exception {
            ActionForward forward = mapping.findForward("createWebsite.page");
            request.setAttribute("userName", "FS");
            return forward;
        }
    
    }
    

    简单的三步就实现了一次request请求分发。
    与jsp/servlet不同的是,将原本web.xml中做的事情分发到struts-config.xml.当然可以配置很多的struts.xml。

  • 相关阅读:
    阻塞队列(BlockingQueue)
    CountDownLatch/CyclicBarrier/Semaphore
    Guava Cache详解
    Java中的常见锁(公平和非公平锁、可重入锁和不可重入锁、自旋锁、独占锁和共享锁)
    CopyOnWriteArrayList详解
    集合类线程安全问题
    原子类的 ABA 问题
    原子类CAS的底层实现
    volatile关键字
    Java 内存模型(Java Memory Model,JMM)
  • 原文地址:https://www.cnblogs.com/stoneFang/p/6715307.html
Copyright © 2020-2023  润新知