struts 2学习进阶 初识struts2(一)
来源:互联网 发布:幼圆字体for mac下载 编辑:程序博客网 时间:2024/05/21 08:03
好久没有写代码了,最近准备学习一下struts2,我准备把我的学习路程写下来。呵呵
Struts2与Struts1有很大的不同,Struts2是与webwork共同开发的,在Struts2中有很多xwork的jar包。看资料上说Struts2的核心是拦截器。
本人菜鸟一个,还指望着敲代码混饭吃呢。废话不多,那就开始吧。
在apache的网站上下载struts2的包,其中apps文件夹中包含了几个struts2的例子程序,docs是文档,lib是包含的jar文件,如果你的jdk是1.4需要在项目中加入j4文件夹的jar包,src是struts2的源文件
在eclipse中新建web工程,将struts2的lib中的struts2-core.jar,xwork.jar,fremarker.jar,ongl.jar引入工程中,
配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
新建两个jsp页面,login.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<form action="login.action" method="post">
username:<input type="text" name="username" />
password:<input type="password" name="password"/>
<input type="submit"/>
</form>
</body>
</html>
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<form action="login.action" method="post">
username:<input type="text" name="username" />
password:<input type="password" name="password"/>
<input type="submit"/>
</form>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
username:$...{requestScope.username }
password:$...{requestScope.password }
</body>
</html>
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
username:$...{requestScope.username }
password:$...{requestScope.password }
</body>
</html>
在src目录下建立LoginAction.java
package com.xie.struts2;
public class LoginAction ...{
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 String execute() ...{
return "success";
}
}
public class LoginAction ...{
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 String execute() ...{
return "success";
}
}
在src下建立struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default">
<action name="login" class="com.xie.struts2.LoginAction">
<result name="success">/success.jsp</result>
<result name="failer">/login.jsp</result>
</action>
</package>
</struts>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default">
<action name="login" class="com.xie.struts2.LoginAction">
<result name="success">/success.jsp</result>
<result name="failer">/login.jsp</result>
</action>
</package>
</struts>
将工程部署到服务器,启动服务器。
上述只是个最简单的一个struts程序
- struts 2学习进阶 初识struts2(一)
- J2EE Struts2 学习笔记001 ---- 初识struts 2
- [Struts2 框架学习] 一、Hello,Struts
- struts征程:1.初识struts2
- Struts2 初识(一)
- 一、初识struts2
- Struts进阶学习
- Struts2框架进阶(一)
- JSON进阶一-初识JSON
- 【Struts2.0】Struts 应用转移到 Struts 2 (一)
- 《深入浅出struts2》--第二章,初识struts
- struts2学习笔记一:一个迷你的Struts
- 初识Struts 2
- Struts2学习2——Struts配置
- Struts 2 学习(一)
- Struts2 逐步成长:(一) 初识Struts2
- struts2学习笔记(二) 初识Struts2
- struts2学习笔记(二) 初识Struts2
- [搜网志]Prototip 基于Prototype的tooltip
- prototype.js 1.4版开发者手册(强烈推荐)
- 做简单的自己,做最好的自己
- 设计模式
- java 时间操作 格式化 时区 系统时间
- struts 2学习进阶 初识struts2(一)
- 从SQL中读字符串时注意用.trim()
- 第二人生的源码分析(三十九)关闭WinXP的错误报告功能
- ubuntu设置环境变量
- ShutDown命令
- Windows XP系统
- AT24C02串行E2PROM的读写
- 什么是模式?
- 记事