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。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?