struts2 框架的基本使用
<?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_3_0.xsd"
id="WebApp_ID" version="3.0"> <!-- 配置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>
<!-- END 配置struts2 的拦截路径 --> </web-app>
<?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.action.extension" value=",action,do" /> -->
<constant name="struts.devMode" value="true" />
<!-- http://localhost:8080/lf/dev_thing --> <package name="myPackage" namespace="/lf" extends="struts-default">
<!-- 配置拦截器 -->
<interceptors>
<!-- 拦截器有三种 -->
<interceptor name="loginInterceptor" class="cn.zr.struts2.interceptor.LoginInterceptor"/>
<interceptor name="twoInterceptor" class="cn.zr.struts2.interceptor.TwoInterceptor"/>
<interceptor name="threeInterceptor" class="cn.zr.struts2.interceptor.ThreeInterceptor"/>
<interceptor-stack name="testStack">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="loginInterceptor"/>
<interceptor-ref name="threeInterceptor"/>
<interceptor-ref name="twoInterceptor">
<param name="excludeMethods">thing</param>
<param name="includeMethods">update</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 换掉默认拦截器 -->
<default-interceptor-ref name="testStack"/> <action name="dev_*" class="cn.zr.struts2.action.LoginAction" method="{1}" >
<!-- 视图 -->
<result name="page" type="dispatcher">
/WEB-INF/jsp/login.jsp
</result>
</action> <action name="test_*" class="cn.zr.struts2.action.Register" method="{1}" >
<result name="lf" type="dispatcher">
/WEB-INF/jsp/register.jsp
</result>
</action> <action name="inf_*" class="cn.zr.struts2.action.InfoAction" method="{1}" >
<result name="info" type="dispatcher">
/WEB-INF/jsp/info.jsp
</result>
</action> </package> </struts>
<?xml version="1.0" encoding="UTF-8"?>
<!-- log4j2.xml文件配置-->
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="com.opensymphony.xwork2" level="debug"/>
<Logger name="org.apache.struts2" level="debug"/>
<Root level="warn">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
package cn.zr.struts2.action; import cn.zr.struts2.pojo.User; public class InfoAction extends SuperAction{ private User user;
private String email; //根据Set的方法注入
public void setUser(User user) {
this.user = user;
} public User getUser() {
return user;
} public void setEmail(String email) {
this.email = email;
} public String getInfo(){
System.out.println("email:"+email);
System.out.println("user:"+user);
return "info";
} }
package cn.zr.struts2.action; public class LoginAction extends SuperAction{ public String thing(){
System.out.println("进入thing的方法.......");
return "page";
} public String add() {
System.out.println("welcome to here======");
System.err.println("request:"+request+",response:"+response+",session:"+session);
return "page";
} public String update() {
System.out.println("**** * update * ****");
return "page";
}
}
package cn.zr.struts2.action; public class Register extends SuperAction{ public String testSome(){
System.out.println("测试+++"); return "lf"; } }
package cn.zr.struts2.action; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware; public class SuperAction implements ServletRequestAware,ServletResponseAware{ public HttpServletRequest request;
public HttpServletResponse response;
public HttpSession session;
@Override
public void setServletResponse(HttpServletResponse response) {
this.response = response; }
@Override
public void setServletRequest(HttpServletRequest request) { this.request = request;
this.session = request.getSession();
} }
package cn.zr.struts2.interceptor; import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor; public class LoginInterceptor implements Interceptor{ @Override
public void destroy() {
// TODO Auto-generated method stub } @Override
public void init() {
// TODO Auto-generated method stub } @Override
public String intercept(ActionInvocation action) throws Exception { System.out.println("拦截前.......");
String result = action.invoke();
System.out.println("=========拦截后.......");
return result;
} }
package cn.zr.struts2.interceptor; import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class ThreeInterceptor extends AbstractInterceptor { @Override
public String intercept(ActionInvocation action) throws Exception { System.out.println("tell me what do you want to do..."); String result = action.invoke(); System.out.println("OK, i let you go..."); return result;
} }
package cn.zr.struts2.interceptor; import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; public class TwoInterceptor extends MethodFilterInterceptor{ @Override
protected String doIntercept(ActionInvocation action) throws Exception {
System.out.println("开始拦截,哈哈哈。。。");
String result = action.invoke();
System.out.println("放你走,哈哈哈。。。");
return result;
} }
package cn.zr.struts2.pojo; public class User { private String username;
private String password;
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 User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
} }
<%@ 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>info starting page</title> </head> <body>
<h1>OK</h1>
</body>
</html>
<%@ 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>login page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<h1>欢迎进入登录页面</h1>
</body>
</html>
<%@ 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>register page</title> </head> <body>
<h1> welcome </h1> <form action="/lf/inf_getInfo.action" method="post">
<label>用户</label><input name="user.username"><br>
<label>密码</label><input name="user.password"><br>
<label>邮箱</label><input name="email"><br>
<input type="submit" value="提交">
</form> </body>
</html>
struts2 框架的基本使用的更多相关文章
- Struts2框架基础
Struts2框架基础 1.Java的框架 1.1.框架简介 在大型项目开发过程中,经常会使用到一些框架,这样做好的好处是能够提高工作效率,在java中最常用的的框架就是SSH,这其实是三个框架的简称 ...
- Struts2框架简介和示例
struts2框架 Struts2是java web的框架,在Java Web开发中,表示层框架,其核心是通过扩展Servlet来帮助处理http请求. Struct2的基本流程 Struct2的框架 ...
- struts2 框架处理流程
struts2 框架处理流程 流程图如下: 注意:StrutsPrepareAndExecuteFilter替代了2.1.3以前的FilterDispatcher过滤器,使得在执行Action之前可以 ...
- 搭建一个简单struts2框架的登陆
第一步:下载struts2对应的jar包,可以到struts官网下载:http://struts.apache.org/download.cgi#struts252 出于学习的目的,可以把整个完整的压 ...
- Struts2框架之-注解开发
Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action类中每个方法的绑定这是重点,在这里先简单看一下配置文件中的简单配置: <span style=" ...
- Struts2 框架的快速搭建
方便myEclipse 手动配置Struts2框架,写下此文,需要的朋友拿走不谢~ 一.引入JAR包 WEB工程->WebRoot->WEB-INF->lib引入Struts2对应版 ...
- struts2框架 初始别
struts2 是webwork和struts合并而来. 1.下载struts2 说明: Full Distribution: 为完整版下载,建议下载它 Example Applications:st ...
- struts2框架快速入门小案例
struts2快速入门: index.jsp------>HelloAction--------->hello.jsp struts2流程 1.导入jar包 struts2的目录结构: a ...
- 搭建struts2框架
struts是一个经典的MVC模式拦截器比过滤器拦截的力度更大 搭建struts2框架1.引入lib包 9个(2.3版本的)common-fileupload;common-io;common-lan ...
- Struts2学习笔记-jsp中引用struts2框架
如果在jsp中需要引用struts2 框架,需在前面加上以下内容 <%@taglib prefix="s" uri="/struts-tags" %> ...
随机推荐
- 剑指Offer面试题:13.合并两个排序的链表
一 题目:合并两个排序的链表 题目:输入两个递增排序的链表,合并这两个链表并使新链表中的结点仍然是按照递增排序的.例如输入下图中的链表1和链表2,则合并之后的升序链表如链表3所示. 二 代码实现 te ...
- python 使用sk_learn 遇到 问题ValueError: Expected 2D array, got 1D array instead:
这里我找到我的问题是: 使用的是一个新的scikit学习版本,它抛出了一个错误,因为在新版本中,所有东西都必须是一个二维矩阵,甚至是一个列或行. 它甚至说:用数组来重塑你的数据.如果您的数据有一个单独 ...
- JDBC 1 利用Statement对数据库进行增删改查
准备工作 1新建po类:User private int id; private String name; private String pwd; set,get方法省略 2 新建UserDao类, ...
- java中的修辞
1.final修辞 final修辞变量分为两种:基本类型(byte,short,int,long,float,double,char,boolean)也是值类型,则一旦初始化,值不能改变:应用类型(S ...
- CentOS虚拟机中安装VMWare Tools
1.单击VMWare的[虚拟机]菜单,选择[安装VMWare Tools]命令 2.接着CentOS系统会自动挂载VMWare Tools,并自动打开,如果没有打开可以自己去图形界面打开VMWare ...
- setContentHuggingPriority和setContentCompressionResistancePriority的使用
当两个UILabel并排显示时,如何设置约束,让 leftLB 和 rightLB 正常显示就很重要了. 方案1:左右两个Label的宽度相同,则约束设置如下: //添加标题约束,左边的label [ ...
- #51单片机#超声波测距(HC-SR04)的使用方法
#51单片机#超声波测距(HC-SR04)的使用方法
- android中MediaPlayer类的用法
用法直接看sample package com.turtle920.androidaudioprocess; import android.media.MediaPlayer; import andr ...
- FPGA论剑(续)
25年之后,第二次华山论剑之时,天下第一的王重阳已然仙逝,郭靖少年英杰刚过二十岁,接东邪黄药师.北丐洪七公300招不败,二人默认郭靖天下第一.南帝段智兴因为出家,法号“一灯”,早已看破名利,故没有参加 ...
- SpringMvc访问Controller去掉do
只需要修改web.xml文件 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xs ...