struts2

来源:互联网 发布:mac键盘使用技巧 编辑:程序博客网 时间:2024/06/11 10:19

开发的环境是JDK1.8 eclipse struts2-2.3.31
1.创建普通的web项目
2.导入jar包
3.修改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_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>struts2_002</display-name>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>  <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></web-app>

4.创建/index.jsp
<%@ taglib prefix=”s” uri=”/struts-tags”%>
struts2的标签库导入

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%><!-- struts2 标签库 --><<%    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 'hello.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><!-- 导入struts2标签库之后就可以使用struts2的标签了 --><!-- struts2 - from标签  --> <s:form  action="index" >        <!-- struts2 - input标签 label = 标签的名字 -->        <s:textfield name="username" label="username" />        <s:textfield name="password" label="password" />  <s:submit /></s:form></body></html>

5.创建/success.jsp文件

<%@ 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 'hello.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>success<br>${sessionScope.username}<br>${requestScope.password}</body></html>

6.创建/error.jsp

<%@ 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 'hello.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>error</body></html>

7.创建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>    <!-- struts2 调试模式  当启用调试模式的时候,改配置文件不需要重新发布服务器-->    <constant name="struts.devMode" value="true" />    <!-- name=是类名 -->    <!--namespace是包名 当包名存在的时,action应该为package_namespace/action_name -->    <!-- extends 是继承的类名 默认集成struts-default -->    <package name="default" namespace="/" extends="struts-default">    <!-- name 是需要处理的请求  -->    <!-- class 接受到该请求后,交给相应的类处理 -->    <!-- class 如果不设置的话,会交给struts2默认的类进行处理 -->        <action name="index" class="com.ifan.action.struts2_02">            <!-- action处理完毕后 返回相应的字符串 不同的字符串对应这不同的jsp页面 -->            <!-- 当namespace设置了之后回到相应的包下面找到相应的jsp                也就是说 namespace="hello"                <result name="success">success.jsp</result>                struts2 会这样找 WebContent/hello/success.jsp 页面                需要特别的注意                下面的请求是到WebContent/success.jsp                /success.jsp 和 success.jsp 是不一样的。                绝对路径            相对路径             -->            <result name="success">/success.jsp</result>            <result name="error">/error.jsp</result>        </action>    </package></struts>

8.创建src/con.ifan.model.user.java

package com.ifan.model;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;    }    @Override    public String toString() {        return "user [username=" + username + ", password=" + password + "]";    }}

9.创建src/com.ifan.action.struts2_002.java

package com.ifan.action;import java.util.Map;import com.ifan.model.user;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;public class struts2_02 extends ActionSupport implements ModelDriven<user>{    //继承了struts2的ActionSupport 里面有很多可以直接用的方法    //实现ModelDriven接口    user u = new user();    //这个方法是Struts2默认调用的方法,当你在配置文件中没有指定方法时,就会默认调用该方法。    @Override    public String execute() throws Exception {        //只有当username == password == aa 时 才会登陆成功        String username = u.getUsername();        String password = u.getPassword();        if("aa".equals(username)&&"aa".equals(password)){            //得到Session对象            Map<String, Object> map = ActionContext.getContext().getSession();            //将username和password存入session            map.put("username", username);            map.put("password",password);            return SUCCESS;        }else{            return ERROR;        }    }    @Override    public user getModel() {        return u;    }}

10.到此位置,struts2-模型驱动-struts2标签的基本使用已经配置完成。

0 0
原创粉丝点击