Action:
struts的控制器action在程序执行过程中只实例化一次,并且是在该action被请求的时候。由于众多的request请求共享一个action实例,所以说struts中的action是线程不安全的,我们在编程的时候针对特定的请求(或者是一些我们需要保护的数据),就必须去做线程同步的操作。
ActionMapping:
actionmapping是用来做什么的,很多的时候(比如说笔者,看来花时间看书,学习还是很重要的)只知道说的在页面跳转的时候,用到了mapping的forward操作,一直以来笔者也没有去关心实现的原理是什么,只是知道怎么用了。作为一个有上进心的码农,我觉得光知道 ‘然’是不够的,还要知道 ‘所以然’来。现在举例来说明一下:
其次,我们在AddStudentAction的execute方法中,添加如下的测试代码:
struts的控制器action在程序执行过程中只实例化一次,并且是在该action被请求的时候。由于众多的request请求共享一个action实例,所以说struts中的action是线程不安全的,我们在编程的时候针对特定的请求(或者是一些我们需要保护的数据),就必须去做线程同步的操作。
ActionMapping:
actionmapping是用来做什么的,很多的时候(比如说笔者,看来花时间看书,学习还是很重要的)只知道说的在页面跳转的时候,用到了mapping的forward操作,一直以来笔者也没有去关心实现的原理是什么,只是知道怎么用了。作为一个有上进心的码农,我觉得光知道 ‘然’是不够的,还要知道 ‘所以然’来。现在举例来说明一下:
现在我做了一个简单的添加student的页面,通过前端的用户表单的入力,经过Actionservlet转交给AddStudentAction来处理,最后调用dao层,插入到数据库。整个流程是相当简单的,不过今天不是说MVC三层架构。主要说明一下ActionMapping如何根据用户的request请求找到了对应的跳转页面。
首先,我们在struts-config.xml配置了如下的信息:
首先,我们在struts-config.xml配置了如下的信息:
<action-mappings>
<action path="/addStudentAction" type="com.infy.AddStudentAction" name="addStudentForm">
<forward name="addStudentSuccess" path="/AddStudentSuccess.jsp"></forward>
<forward name="addStudentFail" path="/AddStudent.jsp"></forward>
</action>
</action-mappings>
看名字我们也差不多可以知道,ActionMapping就是对应的action-mappings里面的信息。
其实我们在web应用启动的时候,就已经在启动并初始化ActionServlet,ActionServlet就读取struts-config.xml中的信息,并把它们保存到各种对象中,而这个时候,struts-config.xml中的action的信息就被保存到了ActionMapping中.
其实我们在web应用启动的时候,就已经在启动并初始化ActionServlet,ActionServlet就读取struts-config.xml中的信息,并把它们保存到各种对象中,而这个时候,struts-config.xml中的action的信息就被保存到了ActionMapping中.
其次,我们在AddStudentAction的execute方法中,添加如下的测试代码:
// just test the actionmapping.
String path = mapping.getPath();//get the action path based on struts-config.xml
String type = mapping.getType();//get the action type based on struts-config.xml
String name = mapping.getName();//get the action form bean name based on struts-config.xml
System.out.println("path=" + path + "\t type=" + type + "\tname=" + name);
// get the forward keys.
String[] forwardNames = mapping.findForwards();
for (String forwardName : forwardNames) {
ActionForward forward = mapping.findForward(forwardName);
// get the path which is blocked in forward.
String forwardPath = forward.getPath();
System.out.println("forwardName=" + forwardName + "\t forwardPath=" + forwardPath);
}
然后,我们在页面上发送http://localhost:8080/struts1/addStudentAction.do请求
最后我们在console端看到的结果如下:
最后我们在console端看到的结果如下:
path=/addStudentAction type=com.infy.AddStudentAction name=addStudentForm
forwardName=addStudentFail forwardPath=/AddStudent.jsp
forwardName=addStudentSuccess forwardPath=/AddStudentSuccess.jsp
这里如何根据一个key values的值去找到或者跳转到对应的页面上去,这个就是struts框架要做的事情了。