struts2入门
struts2入门-第一个例子==》第一个struts2实例——HelloWorld==》Struts2.0搭建和简单实例==>写一个Steuts2的经典入门案例,做了一个登陆程序作为入门例子struts2入门-第一个例子
1.从apache官网下载struts2(v2.2.1)的压缩文件,解压缩
2.打开myeclipse(v8.6),将struts2解压缩后lib下的ongl,strtus2-core,xwork-core,commons-logging,freemaker,javassist(该包需要另外下载,老版本里不需要)这六个必须包导入
在struts2(v2.2.3.1)除了上面包外还需导入commons-io,commons-lang,commons-fileupload
3.配置web.xml(WEB-INF下)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
老版本中用的过滤器是Dispatch类
4.编写用户登录表单
<%@ 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 'index.jsp' starting 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>
<form action="Login" method="post">
<Table>
<Tr>
<td>用户名:</Td>
<td><input type="text" name="username" /></Td>
</Tr>
<Tr>
<td>密码:</Td>
<td><input type="password" name="password" /></Td>
</Tr>
<Tr>
<td><input type="submit" name="submit" value="提交"/></Td>
<td><input type="reset" name="reset" value="重置"/></Td>
</Tr>
</Table>
</form>
</body>
</html>
5.编写LoginAction.java
package com.struts2.action;
public class LoginAction {
private String username;
private String password;
public String execute() throws Exception
{
if(this.getUsername().equals("tom")&&this.getPassword().equals("111111")){
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;
}
}
6.配置strtus.xml(src下)
<?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="strutsqs" extends="struts-default">
<action name="Login" class="com.struts2.action.LoginAction">
<result name="success">/welcome.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
7.结果页面就一句话,就不写了
第一个struts2实例——HelloWorld
内容提要:本文以一个“Hello World”的简单例子,开始struts2之旅。
开发环境:eclipse3.5+jdk6.0+tomcat7.0+struts2
项目目录结构:
详细步骤:
1、打开 Eclipse,新建 Dynamic Web Project,打上项目名struts2,配置Target runtime选择tomcat ,然后直接点 Finish。
2、下载struts2包,我的版本是struts-2.3.8-all,解压,打开lib文件夹,里面都是jar文件,全部选中,复制。
回到 Eclipse,项目下的WebContent-->WEB-INF-->lib目录,粘贴。
3、在 WEB-INF 目录下,新建一个xml文件,文件名web.xml,代码如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_9" 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"> <display-name>Struts Blank</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>index.html</welcome-file> </welcome-file-list> </web-app>
3、在Java Resources -->src目录下新建一个Package,包名为hello,然后在包里新建一个class,文件名为tohello.java,代码如下:
package hello; import com.opensymphony.xwork2.Action; public class tohello { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String execute() { message="go to hello.jsp "; return Action.SUCCESS; } }
4、在Java Resources -->src目录下新建一个xml文件,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> <package name="hello" extends="struts-default"> <action name="GO" class="hello.tohello"> <result> /Helloworld.jsp </result> </action> </package> </struts>
5、在WebContent目录中新建jsp文件,Helloworld.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> ${message}<br/> Hello world ! </body> </html>
6、启动Tomcat Server,打开浏览器,输入http://localhost:8080/struts2/GO.action 或者http://localhost:8080/struts2/GO,回车,则跳转到 Helloworld.jsp。
Struts2.0搭建和简单实例Struts2.0搭建和简单实例
写一个Steuts2的经典入门案例,做了一个登陆程序作为入门例子。
登陆会去交互,并返回页面,比较适合以后扩展的项目,容易理解!
例子:登陆的Struts2案例
环境:
IDE工具:Myeclipse , Tomcat6.0
所需jar包:
struts2-core-2.1.8.1.jar
struts2-json-plugin-2.1.8.1.jar
xwork-core-2.1.6.jar
commons-fileupload-1.2.1.jar
freemarker-2.3.15.jar
junit4.4.jar
ognl-2.6.7.jar
步骤:
1. 创建一个web project项目
2.导入Struts2所需jar包
3.配置web.xml
4.配置struts2.xml
5.写action类(LoginUser.Java)
6.写jsp页面
7.启动tomcat.运行项目
项目最后截图:
开始:
1.创建一个项目
还是截图,项目名称为:Struts2SpExa
2.导入所需jar包
导入所需jar包到项目的lib文件夹下(或者通过Build Path导入也可)
3.在WEB-INF下面的web.xml中配置:
配置拦截器和起始页面
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <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>/WEB-INF/jsp/login.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
配置strust2要调用的action
- <?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 action>
- <struts>
- <package name="todayMZ" extends="struts-default" namespace="/">
- <action name="loginUser" class="todayMZ.user.action.UserAction">
- <result name="success">/WEB-INF/jsp/success.jsp</result>
- <result name="fail">/WEB-INF/jsp/fail.jsp</result>
- </action>
- </package>
- </struts>
这个类主要用于struts2跳转到这个action后。默认执行execute()方法。并根据结果返回字符,然后struts2.xml根据返回的字符跳到相应的页面
- package todayMZ.user.action;
- public class UserAction {
- 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() throws Exception {
- // 只要用户名为todayMZ,密码为1234就可成功登陆
- if ("todayMZ".equals(this.getUsername().trim()) && "1234".equals(this.getPassword().trim())) {
- return "success";
- } else {
- return "fail";
- }
- }
- }
6.写jsp页面
这里有三个jsp页面:login.jsp, success.jsp, fail.jsp
login.jsp(用了简单的标签)
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <%
- 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>
- <title>登陆页面</title>
- </head>
- <body>
- <center>
- <s:form action="loginUser" method="post" namespace="/" >
- <s:textfield name="username" label="用户名"></s:textfield>
- <s:password name="password" label="密码"></s:password>
- <s:submit value="确定"/>
- <s:reset value="重置"/>
- </s:form>
- </center>
- </body>
- </html>
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <%
- 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>
- <title>成功页面</title>
- </head>
- <body>
- <p align="center">登陆成功!!!您的用户名是<s:property value="username"></s:property></p>
- </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>
- <title>失败页面</title>
- </head>
- <body>
- <p align="center">登陆失败!!!</p>
- </body>
- </html>
run项目。在地址栏中输入:http://localhost:8080/Struts2SpExa/
弹出登录界面:(输入toadyMZ,密码123登录成功)
That's OK !
如果过程中报错: No configuration found for the specified action,
那是因为你在login页面使用标签的时候,form表单提交没有指明namespace=??。
-----------------我是分割线-----------------------
本文适合初学者入门案例,步骤无先后规定。也用来方便我记起一些东西。