个人总结:
跟servlet很相似,servlet是在web.xml里面配置,在XXXservlet类里写具体实现-----取调转
Struts2 是在 web.xml声明核心过滤器,具体的配置写在struts.xml里面,而具体实现写在action类里面,套路差不多,只是要注意写业务逻辑代码
首先jsp页面发出请求,struts2核心过滤器拦截该请求,到struts.xml中找到该请求,去对应的类的对应方法处理该请求,这个类是action类,处理的方法返回一个字符串,这个字符串在struts.xml里面相应的能找到对应的jsp或者其他类型的页面,返回请求响应的页面。一次请求结束......
使用框架步骤
一、新建web项目
二、添加Struts2框架支持文件
添加jar包
三、新建jsp页面
引入Struts标签库
<%@taglib prefix="s" uri="/struts-tags" %>
jsp 发送请求
<s:form action="userAction!login.action" method="post">
<s:textfield name="user.name" label="USERNAME"></s:textfield>
<s:textfield name="user.password" label="USERPASSWORD"></s:textfield>
<s:submit value="submit" align="right"></s:submit>
</s:form>
------------- action 这里有三种写法
(1)静态调用login方法:直接写 loginAction.action
直接指定是某个特定方法执行
此时在 struts.xml 中,应当配置 loginAction,并指定是由类中哪个方法执行
<action name="loginAction" class="com.xxx.action.UserAction" method="login"> <result name="loginError">/login.jsp</result> <result name="success">/list.jsp</result> </action>
(2)动态调用login方法:userAction!login.action (Action名字!方法名字)
在struts.xml中只需配置一个action,表单中每个按钮提交事件都可以提交给同一个action,只是对应action类中的不同方法
此时在 struts.xml 中,应当配置 userAction,不需要指定由类中哪个方法执行
<action name="userAction" class="com.xxx.action.UserAction" >
<result name="loginError">/login.jsp</result>
<result name="success">/list.jsp</result>
<result name="update">/update.jsp</result>
</action>
动态方法调用可能会带来安全隐患(通过URL可以执行action中任意方法),官网不推荐这种用法,因此提供了如下配置,默认false,表示禁止动态方法调用
但是按照静态调用,会产生数量庞大的action,所以具体情况还是具体分析
使用动态调用,此时在struts.xml中需要加入
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
(3)通配符:loginAction
在struts.xml里面
<action name="*Action" class="com.xxx.action.UserAction" method=”{1}”> <result name="success">/{1}_success.jsp</result>
<result name="error">/{1}_error.jsp</result>
</action>
loginAction 匹配 *Action , {1} ==> login
registerAction 匹配 *Action , {1} ==> registerss
四、在web.xml中添加过滤器
<!-- struts2 核心过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping>
*.action ---- 表示拦截所有以.action结尾的url,然后交给struts2核心拦截器,
这个核心拦截器再根据struts.xml里的配置去找相应的方法处理
五、创建业务控制器XXXAction类
1、struts2可以在action中自动获得从页面传过来的值,前提是页面的传值的名称要在action类里面先声明变量,添加set、get方法。set是获得页面传的值,get是输出到页面显示。
public class UserAction { //从页面上取的值,作为属性,要加get 和 set 方法 private User user ; public User getUser() { return user; } public void setUser(User user) { this.user = user; } // 响应 userAction!login.action 该请求 public String login() { userDao.userLogin(user.getName(), user.getPassword()); return list(); } public String list(){ userList = userDao.getUserList(); return "success"; // 根据返回的值去 struts.xml 里面 返回相应视图 } }
六、编写struts.xml配置文件
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <include file="struts-default.xml"/> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> <package name="user" namespace="/" extends="struts-default"> <action name="userAction" class="action.UserAction"> <result name="loginError">/login.jsp</result> <result name="success">/list.jsp</result> <result name="update">/update.jsp</result> </action> </package> </struts>
S标签:
<s:formaction="userAction!insert.action"method="post"> <s:textfieldname="user.username"label="用户名"></s:textfield> <s:textfieldname="user.userpassword"label="用户密码"></s:textfield> <s:selectlist="groupList"label="部门" listKey="id"listValue="groupname" name="group.id"> </s:select> <!-- select标签传值貌似不能直接传对象,我是传了对象的ID值过去在根据ID值查到这个对象在处理 --> <s:checkboxlistlist="roleList"label="角色" listKey="id"listValue="rolename" name="roleListArray"value="role" > </s:checkboxlist> <!-- s:checkboxlist 传过去的是一个数组,数组的值默认是listKey的值 --> <s:submitvalue="submit"align="right"></s:submit> </s:form>