因为项目需要,需要使用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。

struts1.x mvc简单例程的更多相关文章

  1. [stm32][ucos][ucgui] 2、LED闪烁、串口、滑块、文本编辑框简单例程

    上一篇:[stm32][ucos] 1.基于ucos操作系统的LED闪烁.串口通信简单例程 * 内容简述: 本例程操作系统采用ucos2.86a版本, 建立了7个任务            任务名   ...

  2. MVC简单分页

    对Car汽车表分页 实现简单分页,放在这里方便查看回顾,自定义每页几条有点问题,有待完善······ 1.新建mvc项目 2.添加linq to sql 数据库连接 3.添加CarBF类 using ...

  3. Spring mvc系列一之 Spring mvc简单配置

    Spring mvc系列一之 Spring mvc简单配置-引用 Spring MVC做为SpringFrameWork的后续产品,Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块 ...

  4. Asp.Net MVC学习总结(一)——Asp.Net MVC简单入门

    一.MVC简单入门 1.1.MVC概念 视图(View) 代表用户交互界面,对于Web应用来说,可以概括为HTML界面,但有可能为XHTML.XML和Applet. 模型(Model) 表示用户对其数 ...

  5. ASP.NET MVC 简单介绍①

    ASP.NET  MVC 简单介绍① 只做了重要描述,内容出自菜鸟教程网站内容. 目录 1布局 2HTML 帮助器 3.Razor 语法 4.添加样式 5.Layout 6. Controllers ...

  6. ASP.NET MVC 简单事务添加

    ASP.NET MVC 简单事务 //实例化查询上下文 using ( BookStoreEntities db = new BookStoreEntities()) { //找到需要价格和名称的数据 ...

  7. ASP.NET MVC 简单分页代码

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  8. MVC学习一:MVC简单流程

    MVC学习一:MVC初次接触 1.MVC简单流程 1.1.服务器接收客户端请求后,解析URL(根据 路由表里配置的URL来分析 类名(控制器名)和方法名)根据请求的类名,创建对应的控制器类对象,并调用 ...

  9. Asp.net MVC 简单分页 自做简单分页

    Asp.net MVC 简单分页:   public static string Pager(int page,int pageSize,int total)         {           ...

随机推荐

  1. ●洛谷P2664 树上游戏

    题链: https://www.luogu.org/problemnew/show/P2664题解: 扫描线,线段树维护区间覆盖 https://www.luogu.org/blog/ZJ75211/ ...

  2. [BZOJ]4644: 经典傻逼题

    某天我觉得一切题目都是那么不可做,于是百度了一下"傻逼题"-- 题目大意:对于图中的任意一个点集(可以为空或者全集),所有恰好有一个端点在这个点集中的边组成的集合被称为割.一个割的 ...

  3. 【SPOJ839】Optimal Marks 网络流

    You are given an undirected graph G(V, E). Each vertex has a mark which is an integer from the range ...

  4. [UOJ UNR #2]积劳成疾

    来自FallDream的博客,未经允许,请勿转载,谢谢. 传送门 区间最大值的题emmmm 想到构建笛卡尔树,这样自然就想到了一种dp f[i][j]表示大小为i的笛卡尔树,根的权值是j的答案. 转移 ...

  5. [luoguP3729]曼哈顿计划EX

    来自FallDream的博客,未经允许,请勿转载,谢谢. 艾登拥有一个计算机网络,每一台计算机都至少有着Intel Xeon E50 v40 + 40路GTX10800Titan的恐怖配置,并由无线网 ...

  6. 解读Raft(一 算法基础)

    最近工作中讨论到了Raft协议相关的一些问题,正好之前读过多次Raft协议的那paper,所以趁着讨论做一次总结整理. 我会将Raft协议拆成四个部分去总结: 算法基础 选举和日志复制 安全性 节点变 ...

  7. 阿里技术一面,Java研发岗

    之前过了个简单的简历面,过了几天后没打来以为凉了,然后昨晚又接到了电话,括号内容是回答说的,理解有限,不一定都对,欢迎纠正-加油每一个牛友们! 阿里一面: 1.学过哪些技术知识呢? 2.说说接口和抽象 ...

  8. MySql查询不区分大小写解决方案(两种)

    当我们输入不管大小写都能查询到数据,例如:输入 aaa 或者aaA ,AAA都能查询同样的结果,说明查询条件对大小写不敏感. 解决方案一: 于是怀疑Mysql的问题.做个实验:直接使用客户端用sql查 ...

  9. CSS :focus 选择器

    :focus 选择器用于选取获得焦点的元素. <!DOCTYPE html> <html> <head> <style> input:focus { b ...

  10. 一口一口吃掉Hibernate(四)——多对一单向关联映射

    hibernate对于数据库的操作,全部利用面向对象的思维来理解和实现的.一般的单独表的映射,相信大家都没有问题,但是对于一些表之间的特殊关系,Hibernate提供了一些独特的方式去简化它. 今天就 ...