Struts2框架运行机制(拦截器,struts标签)
2016-07-28 17:45
471 查看
此实例实现功能:用户需要指定用户名登陆,登陆成功进入相应页面执行操作,否则返回到登陆页面进行登陆,当直接访问操作页面(登陆后才能访问的页面)时则不允许,须返回登陆页面。
login.jsp
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>Insert title here</title> </head> <body> <form action="login.action" method="post"> 用户名:<input type="text" name="userName"/><br/> 密码:<input type="password" name="userPwd"/><br/> <input type="submit" value="提交"/> </form> </body> </html>index.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>Insert title here</title> </head> <body> <s:iterator value="books" var="b"> <s:property value="#b.bookId"/> <s:property value="#b.bookName"/> <s:property value="#b.bookAuthor"/> <s:property value="#b.bookPrice"/> <s:property value="#b.bookInfo"/><br/> </s:iterator> </body> </html>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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Struts05</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 4000 </web-app>拦截器
package com.hellojava.web.tools; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class MyInterceptor extends AbstractInterceptor{ @Override public String intercept(ActionInvocation actioninvocation) throws Exception { System.out.println("myinterceptor拦截开始"); //调用目标Action的execute() 后返回的字符串 //actioninvocation.invoke() 调用客户端请求的action 的 execute() String result=actioninvocation.invoke(); System.out.println("myinterceptor拦截结束"); return result; } }loginAction
package com.hellojava.web.action; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport{ private String userName; private String userPwd; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPwd() { return userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } @Override public String execute() throws Exception { if(!"".equals(userName) && !"".equals(userPwd)){ return this.SUCCESS; }else{ return this.ERROR; } } }LoadBookAction
package com.hellojava.web.action; import java.util.ArrayList; import java.util.List; import com.hellojava.web.entity.Book; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoadBookAction extends ActionSupport { private List<Book> books=new ArrayList<Book>(); public List<Book> getBooks() { return books; } public void setBooks(List<Book> books) { this.books = books; } public String execute(){ for (int i = 0; i < 20; i++) { Book b=new Book(); b.setBookId(i+1); b.setBookName("Java权威指南"+(i+1)); b.setBookAuthor("张三"); b.setBookPrice(22+i); b.setBookInfo("测试"); books.add(b); } return this.SUCCESS; } }Book
package com.hellojava.web.entity; import java.io.Serializable; public class Book implements Serializable{ private int bookId; private String bookName; private String bookAuthor; private double bookPrice; private String bookInfo; public int getBookId() { return bookId; } public void setBookId(int bookId) { this.bookId = bookId; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getBookAuthor() { return bookAuthor; } public void setBookAuthor(String bookAuthor) { this.bookAuthor = bookAuthor; } public double getBookPrice() { return bookPrice; } public void setBookPrice(double bookPrice) { this.bookPrice = bookPrice; } public String getBookInfo() { return bookInfo; } public void setBookInfo(String bookInfo) { this.bookInfo = bookInfo; } }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> <constant name="devMode" value="true"></constant> <package name="struts" namespace="/" extends="struts-default"> <interceptors> <interceptor name="myinter" class="com.hellojava.web.tools.MyInterceptor"></interceptor> </interceptors> <global-results> <result name="error" type="redirect">/error.jsp</result> </global-results> <action name="login" class="com.hellojava.web.action.LoginAction"> <result name="success" type="redirectAction"> <param name="actionName">loadAll</param> <param name="namespace">/</param> </result> </action> <action name="loadAll" class="com.hellojava.web.action.LoadBookAction"> <result name="success">/index.jsp</result> </action> <action name="test" class="com.hellojava.web.action.TestAction"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="myinter"></interceptor-ref> <result name="success">/tag.jsp</result> </action> </package> </struts>标签页
<%@ 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>Insert title here</title> </head> <body> <!-- 使用struts的表单的标签,必须包含在s:form标签内部 表单标签必须要有name属性 --> <s:form action="test.action" method="post"> <s:set name="list" value="{'足球','篮球','排球','兵乓球'}"></s:set> <s:checkboxlist list="#list" name="chk"></s:checkboxlist> <s:date name="date" format="yyyy年MM月dd HH:mm:ss"/> <s:doubleselect label="请选择所在省市" name="province" list="{'四川省','山东省'}" doubleName="city" doubleList="top == '四川省' ? {'成都市', '绵阳市'} : {'济南市', '青岛市'}" /> <s:select label="最高学历" name="education" list="{'高中','大学','硕士','博士'}"/> <s:optiontransferselect label="最喜爱的图书" name="javaBook" list="{'《Java Web开发详解》', '《Struts 2深入详解》', '《Java快速入门》'}" doubleName="cBook" doubleList="{'《VC++深入详解》', '《C++ Primer》', '《C++程序设计语言》'}"/> </s:form> </body> </html>
相关文章推荐
- javaweb——图片上传案例
- Eclipse里面的一些常规设置
- java项目转换依赖等问题
- java-设计模式-观察者模式
- JavaWeb中文件下载
- Java源码-简单计算器界面的AWT实现与Swing实现之比较
- Java 基础 - 泛型
- Java NIO系列教程(四) Scatter/Gather
- 75. Sort Colors
- java设计模式——单例模式(Singleton Pattern)
- spring路径万能公式
- Java fastdfs 防盗链使用问题笔记
- 9、Java ConcurrentModificationException异常原因和解决方法
- SSM(四)WebService入门详解
- java中equals和hashcode方法
- Java调用SQL Server存储过程
- Exception sending context initialized event to listener instance of class ssm.blog.listener.InitBloggerData java.lang.NullPointerException at ssm.blog.listener.InitBloggerData.c
- javaweb学习总结(二十四)——jsp传统标签开发
- spring mvc数据绑定
- web 开发一个能进行人员管理(查询,删除,添加)的应用