Struts 2是一个MVC框架,以WebWork框架的设计思想为核心,吸收了Struts 1的部分优点.Struts 2拥有更加广阔的前景,自身功能强大,还对其他框架下开发的程序提供很好的兼容性。下面我们了解一下syruts2的应用
1.1引入架包
1.2创建loginAction类
package cn.happy.action; import java.util.Map; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import org.apache.struts2.interceptor.SessionAware; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; public class LoginAction implements Action,SessionAware{ private Map<String,Object> map; private String username; private String password; //自动装配 public String execute() throws Exception { if(username.equals("1")&&password.equals("1")){ //解耦方式 (对Servlet Api进行封装 借助ActionContext) Map<String,Object> session=ActionContext.getContext().getSession(); session.put("uname", username); //耦合方式 // HttpSession session2 = ServletActionContext.getRequest().getSession(); // session2.setAttribute("uname",getUsername()); return SUCCESS; }else{ return ERROR; } } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public void setSession(Map<String, Object> map) { this.map=map; } }
1.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> <!-- 修改文件 tomact不用重启 --> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <!-- login action --> <action name="LoginAction" class="cn.happy.action.LoginAction"> <result name="success">login/success.jsp</result> <result name="login">login/login.jsp</result> <result name="error">login/error.jsp</result> </action> <!-- 第一个action --> <action name="HelloWordAction" class="cn.happy.action.HelloWordAction"> <result name="success">index.jsp</result> </action> </package> </struts>
1.4配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name></display-name> <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> <welcome-file-list> <welcome-file>login/login.jsp</welcome-file> </welcome-file-list> </web-app>
1.5编写JSP页面
1.6在这里就展示一下登录页面与登录失败页面
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>登录页面</title> </head> <body> <s:form name="form1" namespace="/" method="post" action="LoginAction"> 请输入用户名: <s:textfield name="username"></s:textfield><br/> 请输入密码: <s:textfield name="password"></s:textfield> <s:reset value="重填"></s:reset> <s:submit value="登陆"></s:submit> </s:form> </body> </html>
在jsp中用到了Struts2 标签
引入
<%@taglib uri="/struts-tags" prefix="s" %>
通用标签(条件,迭代)
1.7 success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% 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 ‘error.jsp‘ starting page</title> </head> <body> <h1>登录失败</h1> <h3>用户名或密码错误,请重新<a href="login/login.jsp">登录</a></h3> <script> var t=10;//设定跳转的时间 setInterval("refer()",1000); //启动1秒定时 function refer(){ if(t==0){ location="http://localhost:8080/Day-login2-struts2/login/login.jsp"; //跳转的链接地址 } document.getElementById(‘show‘).innerHTML=""+t+"秒后跳转到登录"; // 显示倒计时 t--; // 计数器递减 } </script> <span id="show"></span> </body> </html>
1.8结果展现
1.9登录成功 用户名:1 密码:1
1.10 登录失败 10秒后会跳会登录
2.0拓展
当我们用到的属性多的时候都写在loginAction类中就会感觉到特别的凌乱,这个时候我们就可以创建一个类来管理这些属性(例:user)
package cn.happy.entity; public class User { private String name; private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } private String username; private String password; public String getUsername() { return username; } public String getPassword() { return password; } public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } }
这时候只在loginAction类中植入这个类就行了
private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; }
这样代码就会显得更加的清晰,岁然这样做会让我们的眼前一亮,事物都有两面性有利就有弊。我们用到的属性名前面都要加上管理它们类的名称(如:user.getUsername)
public class LoginAction implements Action,ModelDriven<User>{ @Override public String execute() throws Exception { if(user.getUsername().equals("1")&&(user.getPassword().equals("1"))){ return SUCCESS; }else{ //失败回到登录 return LOGIN; } } private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public User getModel() { // TODO Auto-generated method stub return user; } }
在1.2中我们可以看到加粗字体的语句解耦与耦合的应用(在下一篇博客中会有详细解释 解耦与耦合的你我他)
//解耦方式 Map<String,Object> session=ActionContext.getContext().getSession(); session.put("uname", username); //耦合方式 HttpSession session2 = ServletActionContext.getRequest().getSession(); session2.setAttribute("uname",getUsername());
首先我们先要在登录成功页面配置一道(success.jsp)
<body> 欢迎你!${uname} </body>
实现效果 用户名为1
时间: 2024-11-05 14:55:51