标签:
以一个修改的小例子来阐述Struts2的工作流程
页面from表单
<form action="ModifyPassword.action" method="post"> <table cellspacing="0" cellpadding="5" width="100%" align="left"> <tr> <td width="20%">新密码:</td> <td><input type="text" name="login_password" id="login_password" maxlength="7"/></td> </tr> <tr> <td width="20%">确认新密码:</td> <td><input type="text" name="passwordnew" id="passwordnew" maxlength="7"/></td> </tr> <tr> <td><input type="hidden" name="id" value="${admin.id}"/></td> <td><input type="submit" name="submit" value="修改" onclick="check();"/> <input type="button" name="Input" value="返回" onclick="window.location.href=‘website_set.jsp‘;" class="button"/></td> </tr> </table> </form>
Struts.xml
from表单中的Modifypassword
<action name="ModifyPassword" class="com.yinguo.action.admin.ModifyPassword"> <result name="success">success.jsp</result> <result name="error">error.jsp</result> </action>
写对应的action方法
public class ModifyPassword extends ActionSupport{ private static final long serialVersionUID = 1L; private int id; private String login_password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getLogin_password() { return login_password; } public void setLogin_password(String loginPassword) { login_password = loginPassword; } public String execute() { AdminServiceImpl adminService = new AdminServiceImpl(); HttpServletRequest request = ServletActionContext.getRequest(); HttpSession session = request.getSession(); Admin admin = (Admin) session.getAttribute("admin"); if (admin == null) { return "fail"; } Admin admin1 = new Admin(); admin1.setLoginPassword(login_password); admin1.setId(id); try { if (!(adminService.modifyPassword(admin1))) { return ERROR; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return SUCCESS; } }
表单中的字段name要和action中的字段对应,每个action都要继承actionSupport ,必须有个execute方法并且是String 类型。
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>/*</url-pattern> </filter-mapping>
标签:
原文地址:http://my.oschina.net/isxiaoge/blog/485523