struts1.x mvc简单例程
来源:互联网 发布:windows loader uefi 编辑:程序博客网 时间:2024/05/21 10:52
因为项目需要,需要使用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。
0 0
- struts1.x mvc简单例程
- 简单配置文件struts1.x
- struts1.x的简单流程
- struts1.x、struts2.x、Spring MVC原理总结
- 传智播客 struts1.x 简单登录示例
- Struts1.x最简单例子-温故知新
- struts1.x的简单工作流程
- struts1 - MVC
- Studs MVC Framework-----PHP的Struts1.X实现
- struts1.x
- Struts1.x
- Struts1.x
- Struts1.x系列教程(2):简单的数据验证
- Struts1.x系列教程(2):简单的数据验证
- 在Spring2下针对struts1.x框架的简单封装
- Struts1.x系列教程(2):简单的数据验证
- Struts1.x系列教程(2):简单的数据验:
- MVC模式的讨论与struts1.x和Struts2的比较
- android 限制输入字符数!看清楚:是限制字符数,不是字数!中英混排?就算emoji也不怕啦!
- My_Graph2 DFS BFS
- 第八周--项目5计数的模式匹配
- Lasso
- Docker在windows下无法开启伪终端解决方法
- struts1.x mvc简单例程
- eclipse自动补全设置
- 与任正非的一次花园谈话
- Volley源码分析和学习
- Java数组常用排序算法实现复习
- 【PAT】1067. Sort with Swap(0,*) (25)
- 一个简短的epoll服务器示例, 监听5000个端口, 使用线程池
- JAVA各种引用(Reference)的汇总
- TabHost页卡