struts2的运行原理图解:网上图片
首先我们
A :服务器收到客户端请求request
B: ActionMapper 查看这个请求是否归struts2处理,如果是。
C 然后进行一些简单的过滤FilterDispatcher过滤操作。
D ActionProxy代理进行去查询struts.xml获得哪个导演Action invocation需要做出响应,之后导演做出一系列的准备(inteceptor:如数据set工作等 场景布置),
E 之后就是Action表演,并产生一个Action 结果Result
F 导演查看并配置这个Result结果应该做出怎样的回复。
G 撤掉场景布置(还原数据)
H :做出相应。HttpServletResponse
<!-- 一个Action如果没有写类路径,则默认采用ActionSupport方法
如果没有写method,则默认运行execute方法,
-->
<!-- 一个Result如果没有写名字的话,默认为success -->
<!-- 一个Action如果没有写类路径,则默认采用ActionSupport方法 如果没有写method,则默认运行execute方法, --> <action name="doIt"> <!-- 一个Result如果没有写名字的话,默认为success --> <result>/ok.jsp</result> </action>
利用struts更改原sms项目代码表现层:
部分代码:
StudentAction.java
/** * 工 程 名:SMS-SSH-20180524 <br> * 文 件 名:StudentAction.java <br> * 工具包名:edu.fjnu.training.action <br> * 功能描述:TODO <br> * 创建时间:2018年5月24日 下午1:29:41 <br> * 版本信息:V1.0 * @创建人:Zhou Kailun */ package edu.fjnu.training.action; import java.io.File; import edu.fjnu.training.domain.Student; import edu.fjnu.training.service.StudentService; import edu.fjnu.training.service.StudentServiceImpl; /** * 类名:StudentAction <br> * 功能描述: <br> * 创建日期:2018年5月24日 下午1:29:41 <br> * 修改备注: * @作者信息:Zhou kailun <br> */ public class StudentAction extends BaseAction { /**学生信息对象*/ private Student stu; /**上传图片名字:与jsp请求网页名字相同,后面两条属性必须一样写入stuPicFilenName,stuPicContentType */ private File stuPic; private String stuPicFileName; private String stuPicContentType; /**<p>构造函数:</p><br><br> * <p>描述:</p><br> */ public StudentAction() { // TODO Auto-generated constructor stub } public String toReg()throws Exception{ return "reg_page"; } public String reg()throws Exception{ StudentService studentService=new StudentServiceImpl(); studentService.addStudent(stu); return "loadall"; } public Student getStu() { return stu; } public void setStu(Student stu) { this.stu = stu; } public File getStuPic() { return stuPic; } public void setStuPic(File stuPic) { this.stuPic = stuPic; } public String getStuPicFileName() { return stuPicFileName; } public void setStuPicFileName(String stuPicFileName) { this.stuPicFileName = stuPicFileName; } public String getStuPicContentType() { return stuPicContentType; } public void setStuPicContentType(String stuPicContentType) { this.stuPicContentType = stuPicContentType; } }
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!-- /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ --> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false"></constant> <constant name="struts.devMode" value="false"></constant> <!-- 在网页中以开发模式打开,用于程序员直接在网页中查看运行情况 --> <constant name="struts.action.extension" value="do,action"></constant> <!-- 如果没有显示配置,默认为action, --> <constant name="struts.ui.theme" value="simple"></constant><!-- Struts 生成的UI界面代码风格,我们一般希望是自己能控制的。但是也可以一局各单位公司不同,有不同风格 --> <constant name="struts.i18n.encoding" value="utf-8"></constant><!-- struts编码方式为utf-8 --> <!-- http://localhost:8080/strutsDemo/work/demo.do 1.struts过滤器拦截到url地址,获得该url的后缀,如果是*.do,则为struts处理范畴。去掉猴嘴,获得实体路径。 2.取得包路径(最后一个/之前的路径)/work 3.获得action的名字demo,然后找到这个action,创建该action的实例。执行execute方法一次。 --> <!-- sms系统基础包 --> <package name="smsPkg" abstract="true" namespace="/" extends="struts-default"> </package> <!-- 学生信息管理 --> <package name="stuPkg" namespace="/student" extends="smsPkg"> <action name="toReg" class="edu.fjnu.training.action.StudentAction" method="toReg"> <result name="reg_page" type="dispatcher">../jsps/student/reg_student.jsp</result> </action> <action name="work" class="edu.fjnu.training.action.StudentAction" method="work"> </action> <action name="reg" class="edu.fjnu.training.action.StudentAction" method="reg"> <result name="list_page">../jsps/student/list_student.jsp</result> </action> </package> <!-- 教师信息管理 --> <package name="teacherPkg" namespace="/teacher" extends="smsPkg"> </package> </struts>
reg_student.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ include file="/jsps/common-tags.jsp"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'reg_student.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <%-- <link rel="stylesheet" type="text/css" href="../../../css/style.css"> --%> <%-- <link rel="stylesheet" type="text/css" href="/sms/css/style.css"> --%> <%-- <link rel="stylesheet" type="text/css" href="<%=basePath%>/css/style.css">--%> <link rel="stylesheet" type="text/css" href="<c:url value="/css/style.css"/>"> <script type="text/javascript" src="<c:url value="/js/common.js"></c:url>"></script> </head> <body> <h3>新生登记(2018年5月22日08:02:55)</h3> <s:form action="reg" namespace="/student" method="post" enctype="multipart/form-data"> <img id="stuPhoto" style="float:right;" alt="学生图片" src="<c:url value="/pics/default.jpg"/>" width="160px" height="160px" /> <!-- 学生信息 学号: --> <div> <label>学号:</label> <s:textfield name="stu.stuNo"></s:textfield> </div> <!-- 学生信息 密码: --> <div> <label>姓名:</label> <s:textfield name="stu.stuName"></s:textfield> </div> <div> <label>相片:</label> <s:file name="stuPic" onchange="$('stuPhoto').src=this.value"></s:file> </div> <div> <label>成绩:</label> <s:textfield name="stu.stuMark"></s:textfield> </div> <div> <label>性别:</label> <s:radio name="stu.stuSex" list="#{'ma':'男','fe':'女'}"></s:radio> </div> <div> <label>爱好:</label> <s:checkboxlist name="stu.stuHobbies" list="#{'cm':'爬山','rd':'阅读','sw':'游泳','rn':'跑步'}"></s:checkboxlist> </div> <div> <label>籍贯:</label> <s:select name="stu.stuOrigin" list="#{'xm':'厦门','fz':'福州','nd':'宁德','pt':'莆田'}"></s:select> </div> <div> <label>备注:</label> <s:textarea name="stu.stuMemo" rows="6" cols="60" ></s:textarea> </div> <div> <s:submit value="注册登记"/> </div> </s:form> <a href="<c:url value="/smsMgr" />?act=main">首页</a> <%-- <jsp:include page="../../css/footer.jsp"></jsp:include>--%> </body> </html>