记录下,struts2的搭建过程:
1核心jar包:
struts-2.1.8appsstruts2-blank-2.1.8.war 解压后 在struts2-blank-2.1.8WEB-INFlib下就是
2配置文件白板
web.xml在 struts2-blank-2.1.8WEB-INFweb.xml
struts.xml在 struts2-blank-2.1.8WEB-INFclassesstruts.xml
3 struts.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> </package> <package name="user" namespace="/user" extends="struts-default"> <action name="*_*" class="action.{1}Action" method="{2}"> <result name="login_success">/user/login_success.jsp</result> <result name="login_failure">/user/login.jsp</result> <result name="logout_success">/user/logout.jsp</result> </action> </package> </struts>
4 action.UserAction.java控制文件
package action; import org.apache.struts2.interceptor.validation.SkipValidation; import service.UserDAO; import servie.impl.UserDAOImpl; import com.opensymphony.xwork2.ModelDriven; import entity.User; public class UserAction extends SuperAction implements ModelDriven<User>{ private User user = new User(); private static final long serialVersionUID = 1L; public String login(){//用户登录动作 UserDAO uDao = new UserDAOImpl(); if (uDao.userLogin(user)) { //在session中保存登陆成功后的信息 session.setAttribute("loginUserName", user.getUsername()); return "login_success"; }else { return "login_failure"; } } //注销用户 public String logout() { if (session.getAttribute("loginUserName") != null) { session.removeAttribute("loginUserName"); } return "logout_success"; } public User getModel(){ //ModelDriven就必须实现getModel这个方法, //该方法返回的是你的ModelDrivern泛型中设定的实体类 return this.user; } }
5 访问注意命名空间,namespace="/user",表单的访问登陆控制页面的路径为
<form action="<%=path %>/user/User_login.action" method="post">
6 index.jsp跳转页面语句是
<% response.sendRedirect(request.getContextPath() + "/user/User_login.action"); %>
/user是命名空间,.action结尾的为被strut.xml配置文件拦截,跳转到UserAction控制文件的login()方法。