Struts2框架技术
2016-04-24 23:03
477 查看
Struts2是基于WebWork的核心思想上产生的全新框架,在Java Web开发领域之中占有十分重要的地位,随着JSP技术的成熟,越来越多的程序开发人员专注于MVC框架,Struts2得到了广大程序员的青睐,本篇博客将对Struts2框架技术进行详细讲解。
创建Struts2项目步骤如下:
首先,下载Struts2的jar包:下载地址:https://struts.apache.org/,下载最小包解压即可。
然后创建一个WebProject项目并导入struts2所需jar包,下面给出创建工程目录图如下:
接下来给出详细代码:
配置web.xml文件,创建在WEB-INF目录下面
配置struts.xml文件,创建在WEB-INF/classes目录下面
User实体类
项目登录页面login.jsp
补充:
OGNL表达式:
创建Struts2项目步骤如下:
首先,下载Struts2的jar包:下载地址:https://struts.apache.org/,下载最小包解压即可。
然后创建一个WebProject项目并导入struts2所需jar包,下面给出创建工程目录图如下:
接下来给出详细代码:
配置web.xml文件,创建在WEB-INF目录下面
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Test</display-name> <!-- 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> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app>
配置struts.xml文件,创建在WEB-INF/classes目录下面
<?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> <constant name="struts.devMode" value="true" /> <!-- 把它设置为开发模式,发布时要设置为false --> <constant name="struts.i18n.encoding" value="UTF-8" /> <!-- 编码格式 --> <!-- 包属性name随便起,当namespance的值是"/"的时候,请求url中不需要加入任何的路径,直接填写action中的name属性值,即"helloWorld" 当namespance的值是"/test"的时候,请求url中需要加入"test/"路径前缀,后面跟上action中的name属性值, 即"test/helloWorld" action中的name属性随便起class为该action对应的类,result中的name为对应类执行execute方法后返回的字符串值,里面的内容为返回的jsp页面路径,注意"/"代表寻找当前路径的上一层目录下的子文件 --> <package name="test" extends="struts-default" namespace="/"> <interceptors> <!-- 自定义权限拦截器--> <interceptor name="authortityInterceptor" class="com.kj.test.AuthortityInterceptor"></interceptor> <interceptor-stack name="myStack"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="authortityInterceptor"></interceptor-ref> </interceptor-stack> </interceptors> <!-- 全局配置返回login字符串时对应的login.jsp视图 --> <global-results> <result name="login">login.jsp</result> </global-results> <!-- 登录action不设置权限拦截器,登录后保存User信息,然后当浏览器请求其他Action的时候需要经过权限拦截器的处理,只有当用户信息不为空时才返回对应的Action请求结果 --> <action name="login" class="com.kj.test.UserAction" method="login"> <result name="success">index.jsp</result> </action> <!-- 通配符配置:如地址栏输入helloWorld_User_login,这里代表:包_类_方法,所以要:小写_大写_小写方式,配置通用Action,并在每个action中加入myStack拦截器 --> <action name="*_*_*" class="com.kj.test.{2}Action" method="{3}"> <result name="success">/{1}/{3}_{2}Success.jsp</result> <interceptor-ref name="myStack"></interceptor-ref> </action> </package> </struts>
User实体类
package com.kj.test; import java.io.Serializable; /** * 用户实体类 * @author KJ * */ public class User implements Serializable{ private static final long serialVersionUID = 7178219894611819140L; private Long id; private String name; private String account; private String password; public Long getId() { return id; } public User () { } public User (String account) { this.account = account; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }权限拦截器
package com.kj.test; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; /** * struts自定义拦截器 * @author KJ * */ public class AuthortityInterceptor extends AbstractInterceptor{ private static final long serialVersionUID = -209271634535003554L; @Override public String intercept(ActionInvocation invocation) throws Exception { ActionContext ctx = invocation.getInvocationContext(); Map<String, Object> session = ctx.getSession(); //拦截没有登录的用户 User user = (User) session.get("user"); if (user == null || user.getAccount() == null) { session.put("message", "您还没有登录,请先登录系统"); return "login"; } return invocation.invoke(); } }action类
/** * */ package com.kj.test; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; /** * @author KJ * */ public class UserAction extends ActionSupport { private static final long serialVersionUID = -7113951589870848964L; private static final String MESSAGE = "这是第一个Struts2程序,Hello, Struts2."; private String userName; private String message; /** * @return the message */ public String getMessage() { return message; } /** * @param message * the message to set */ public void setMessage(String message) { this.message = message; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String execute() throws Exception { setMessage(MESSAGE); return "success"; } public String login() throws Exception { setMessage(MESSAGE); Map<String,Object> session = ActionContext.getContext().getSession(); session.put("user", new User(userName)); return "success"; } }
项目登录页面login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录页面</title> </head> <body> <h1>登录页面</h1> <form action="login" method="post"> <input type="hidden" name="userName" value="testUser"/> <input type="submit" value="登录"/> </form> </body> </html>首页index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>网站首页,经过struts2框架跳转到另外一个页面</title> </head> <body> 网站首页,<a href="helloWorld_User_login">经过struts2框架跳转到另外一个页面</a> </body> </html>登录成功跳转页login_UserSuccess.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Hello World!</title> </head> <body> <h2><s:property value="#session.user.account" />,恭喜您,<s:property value="message" /></h2> </body> </html>
补充:
OGNL表达式:
<s:property value="#session.user" />值栈的属性不需要#前缀,非值栈Action属性如:request,session,application.parameters,attr等需要#前缀。
相关文章推荐
- 20145204《Java程序设计》第8周学习总结
- Spring.Net IOC基本应用和在MVC4中的应用
- java并发之Semaphore(信号量)
- spring框架学习笔记(六)
- 20145104张家明 《Java程序设计》第8周学习总结
- java并发之CyclicBarrier(障碍器)
- [改善Java代码]使用静态内部类提高封装性
- 20145212 《Java程序设计》第8周学习总结
- 20145320《Java程序设计》第三次实验报告
- Java内存模型
- Java基础知识:走进Java的世界
- spring security 登录、权限管理配置
- 20145302张薇《Java程序设计》第八周学习总结
- 趣味素数-哥德巴赫猜想-java
- 20145325张梓靖 《Java程序设计》第8周学习总结
- java第八周学习总结
- 《Java程序设计》第8周学习总结
- Java企业实训 - 01 - Java前奏
- [改善Java代码]构造函数尽量简化
- Java企业实训 - 01 - Java前奏