Struts2中一个简单的入门实例
一.基本步骤:
?
1.准备struts2包:
?
最基本的五个jar包拷贝到WEB-INF的lib目录下,具体包名如下:
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.11.2.jar
xwork-2.0.5.jar
?
2.在web.xml注册和映射FilterDispatch
?org.apache.struts2.dispatcher.FilterDispatcher
?
3.login.jsp界面
?<form action="user!login.action">
?
4. 写UserAction类,不需要继承
?
?
5. 在一个配置文件struts.xml文件,放在src下面
??
二.一个action处理多个请求
?? 1. 在action中把execute方法删除或改名
?? 2. 创建自己的方法
?? 3. <form ?action="user!login.action"
????? 这里本来是action="user.action"
????? 这里的login就是方法名
?????
三.怎么使用request/ session
?? 1. 只要在action写一个属性,并生成set/get就会自动保存到request
?? 2. ActionContext.getContext().put("test2", "test2");???
?
?
具体代码如下:
建立项目名称:Struts2.
在src目录下.建立包com.struts2.action
在包下面建立一个UserAction类;
具体代码如下:
?
package com.struts2.action;import com.opensymphony.xwork2.ActionContext;public class UserAction {private String id;private String username;private String password;public String getId() {return id;}public void setId(String id) {this.id = id;}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;}/** * 登陆方法; * @return String; */@SuppressWarnings("unchecked")public String login(){this.id = "1001";//判断;if(this.username.equals("zhouhaitao") && this.password.equals("7290783")){//ActionContext.getContext.相当于request.setAttribuet()一样;ActionContext.getContext().put("id",id);//保存到session当中;ActionContext.getContext().getSession().put("name", this.username+"登陆成功!");return "yes";}else{return "no";}}/** * 注册方法; */public String regester(){System.out.println("调用注册的方法.开始注册!");System.out.println(this.id);System.out.println(this.username);System.out.println(this.password);ActionContext.getContext().put("name",this.username+"注册成功!");return "yes";}}
?
?
之后,在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><!-- 可以引用其他的配置文件 --><include file="struts-default.xml"></include><!-- name代表包的名称 extends代表引用其他的默认文件,namespace代表命名空间,可选--><package name="com.struts2.action" extends="struts-default"><!-- name 代表action名称,与struts1的path类似 --><action name="user" type="dispatcher">success.jsp</result><result name="no" type="redirect">failure.jsp</result></action></package></struts>
?
?
?
在WebRoot目录下:创建一个login.jsp页面
具体代码如下:
<%@ page language="java" import="java.util.*" 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>Login Page</title><script type="text/javascript">//提交;function onSubmitLogin(){var username = document.getElementById("username");var password = document.getElementById("password");var bol = false;if(username.value == ""){alert("用户名不能为空!");username.focus();bol = false;return;}if(password.value == "" || password.value.length <=0){alert("密码不能为空!");password.focus();bol=false;return;}if(bol=true){document.getElementById("myform").submit();}}</script></head><body onload="document.getElementById('username').focus()"><center><h1>用户登陆</h1><form action="user!login.action" method="post" name="myform" id="myform">用户名:<input type="text" name="username"> <Br/>密码: <input type="password" name="password"><Br/><Br/><input type="button" value=" 登 陆 " onclick="onSubmitLogin()"> <input type="reset" value=" 重 置 "></form></center></body></html>
?
建立一个success.jsp 页面 和failure.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>Insert title here</title></head><body><font color="red"><h1>恭喜:${request.name }</h1></font><Br/><a href="http://localhost:8080/Struts2/regist.jsp">返回注册页面</a> <a href="http://localhost:8080/Struts2/login.jsp">返回登陆页面</a></body></html>
?
?
failure页面:
<%@ 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>Insert title here</title></head><body><font color="red"><h1>抱歉,登陆失败!!</h1></font><br/><a href="http://localhost:8080/Struts2/login.jsp">返回登陆页面</a></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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>Struts2</display-name> <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> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
?
在tomcat中部署,发布启动服务.
在流量器中运行:
http://localhost:8080/Struts2/login.jsp
?
然后,输入正确的用户名和密码.
ok。登陆成功!