java 从零开始,学习笔记之基础入门(三十八)

时间:2021-01-18 19:43:17

Struts2_实战演练(上)

三个准备:

1.导入Struts2库(jar包)

2.添加核心配置文件struts.xml

3.配置properties文件

 

准备二,配置struts2核心filter

Web.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appversion="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">

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

 

  <filter>

    <filter-name>filterDispatcher</filter-name>

    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

  </filter>

 

  <filter-mapping>

    <filter-name>filterDispatcher</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

 

</web-app>

 

 

准备三:配置properties文件

struts.properties

struts.action.extension=do

 

第一个sturts框架

登录框:

Index.jsp

<%@ page language="java"import="java.util.*"pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <basehref="<%=basePath%>">

   

    <title>My JSP 'index.jsp' starting page</title>

    <metahttp-equiv="pragma"content="no-cache">

    <metahttp-equiv="cache-control"content="no-cache">

    <metahttp-equiv="expires"content="0">   

    <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

    <metahttp-equiv="description"content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css"href="styles.css">

    -->

  </head>

 

  <body>

    用户登录

    <formaction="login.do"name="userform"id="userform">

    用户姓名:<inputtype="text"name="userName"id="uname"/>

     <br/>

    用户密码:<inputtype="password"name="userPwd"id = "pwd"/>

     <br/>

     <inputtype="submit"value="用户登录"/>

    </form>

  </body>

</html>

 

登录跳转需经过struts配置文件

Struts.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEstruts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <packagename="loginpackage"extends="struts-default">

        <actionname="login"class="com.softeee.action.LoginAction">

            <resultname="success">success.jsp</result>

        </action>

    </package>

   

    <!-- 在主配置文件中可以包含分支文件 -->

    <includefile="book.xml"></include>

   

    <includefile="auth.xml"></include>

   

    <includefile="commoper.xml"></include>

</struts>

Book.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEstruts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <packagename="bookpackage"extends="struts-default">

        <actionname="search"class="com.softeee.action.BookOperAction"method="searchBook">

            <resultname="success">booklist.jsp</result>

        </action>

       

        <actionname="delete"class="com.softeee.action.BookOperAction"method="delBook">

            <resultname="success">booklist.jsp</result>

        </action>

       

        <actionname="update"class="com.softeee.action.BookOperAction"method="updateBook">

            <resultname="success">bookUpdate.jsp</result>

        </action>

       

        <!-- 完成修改操作后返回到数据显示页面 -->

        <actionname="upBook"class="com.softeee.action.BookOperAction"method="upBook">

            <resultname="success">bookList.jsp</result>

        </action>

    </package>

   

</struts>

 

通过配置文件跳转到LoinAction.java中并找到默认的execute方法

LoginAction.java

package com.softeee.action;

 

import java.util.Map;

 

import org.apache.struts2.interceptor.SessionAware;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class LoginAction extends ActionSupportimplements SessionAware{

   

    Map session;

   

    //用户姓名

    private StringuserName;

    //用户密码

    private StringuserPwd;

   

    public LoginAction() {

        System.out.println("实例化了");

    }

   

    public String execute()throws Exception{

        System.out.println("登录的用户名是:"+userName+"  密码:"+userPwd);

        //session中存入用户信息

        session.put("uname",userName);

        returnSUCCESS;

    }

   

    public String getUserName() {

        returnuserName;

    }

    public void setUserName(String userName) {

        this.userName = userName;

    }

    public String getUserPwd() {

        returnuserPwd;

    }

    public void setUserPwd(String userPwd) {

        this.userPwd = userPwd;

    }

 

    public void setSession(Map<String, Object> arg0) {

        this.session=arg0;

    }

 

}

 

 

搜索书

Search.jsp

<%@ page language="java"import="java.util.*"pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <basehref="<%=basePath%>">

   

    <title>My JSP 'searchbook.jsp' starting page</title>

   

    <metahttp-equiv="pragma"content="no-cache">

    <metahttp-equiv="cache-control"content="no-cache">

    <metahttp-equiv="expires"content="0">   

    <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

    <metahttp-equiv="description"content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css"href="styles.css">

    -->

 

  </head>

 

  <body>

    查询书本信息

    <formaction="search.do"name="searchform"id="searchform">

    书名:<inputtype="text"name="book.bookName"id="bookName">

         <inputtype="submit"value="查询">

    </form>

  </body>

</html>

 

BookOperAction.Java

Book.java

package com.ibm.dto;

 

public class Book {

    //书本编号

    private int bookId;

    //书本名称

    private StringbookName;

   

    public int getBookId() {

        returnbookId;

    }

    public void setBookId(int bookId) {

        this.bookId = bookId;

    }

    public String getBookName() {

        returnbookName;

    }

    public void setBookName(String bookName) {

        this.bookName = bookName;

    }

   

   

 

}

 

 

package com.softeee.action;

 

import java.util.ArrayList;

import java.util.List;

 

import com.opensymphony.xwork2.ActionSupport;

import com.ibm.dto.Book;

import com.ibm.factory.HibernateSessionFactory;

 

public class BookOperAction extends ActionSupport {

   

    private Bookbook;

   

    private List<Book>bookList;

   

    public List<Book> getBookList() {

        returnbookList;

    }

 

    public void setBookList(List<Book> bookList) {

        this.bookList = bookList;

    }

 

    public Book getBook() {

        returnbook;

    }

 

    public void setBook(Book book) {

        this.book = book;

    }

   

    public String searchBook(){

        System.out.println("查询的书名是:"+book.getBookName());

       

        HibernateSessionFactory.getSession();

       

        //以下部分模拟从数据库中查询书本并存放到list对象中进行传递

        bookList = new ArrayList<Book>();

        for (int i = 0; i < 5; i++) {

            Book book = new Book();

            book.setBookId(i);

            book.setBookName("孙子兵法"+i);

            bookList.add(book);

        }

       

        return"success";

    }

   

    public String delBook(){

        System.out.println("删除的书本的编号是:"+book.getBookId());

        //补全删除部分的代码

        return"success";

    }

   

    public String updateBook(){

        System.out.println("修改的书本的编号是:"+book.getBookId());

        //伪代码部分 真实应用中从数据库中查询与之对应的结果

        book=new Book();

        book.setBookId(1);

        book.setBookName("货币战争");

        return"success";

    }

   

    public String upBook(){

        //调用hibernatedao完善此部分

        System.out.println("修改部分");

        return"success";

    }

 

}

对比第一种取值方式(

<input type="text" name="userName" id="uname"/>

),下面这种方式是对bookname进行封装成book对象,即

<input type="text" name="book.bookName" id="bookName">

)在Action中需定义一个Book对象得到bookname

 

配置通配符

1.当struts2解析到adduser方法时,就会自动去找useradd方法

2.直接转向到adduser.jsp

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEstruts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <packagename="useroperpackage"extends="struts-default">

        <actionname="*user"class="com.softeee.action.UserOperAction"method="{1}user">(1”代表第一个“*)

            <resultname="success">{1}user.jsp</result>

        </action>

    </package>

   

</struts>