2018-09-29:JSP与Struts2框架配置

2018-09-29  本文已影响0人  神坛下的我

TestCustomer.java

import java.util.List;

import org.hibernate.Session;

import repairsystem.entity.Customer;
import repairsystem.factory.HibernateSessionFactory;

public class TestCustomer{

    public static void main(String[] args) {
        Session session = HibernateSessionFactory.getSession();
        List<Customer> customer = session.createQuery("From Customer").list();
        for(Customer c:customer){
            System.out.println(c.getCid()+"\t"+c.getCname()+"\t"+c.getCpassword()+"\t"+c.getCphone()+"\t"+c.getCimg());
        }
    }

}


JSP

  1. page 页面设置 字符集 contentType 当前页面输出的字符集
    pageEnding 当前页面编写的字符编码集
    引入java类
  2. include 引入其他页面的内容(静态引入:之引入另外页面的结果,可以有重复
    变量)
  3. taglib
    %>
<jsp:forward page="list2.jsp"></jsp:forward>
等价于
<%
  request.getRequestDispatcher("list2.jsp").forward(request,response);
%>

response 和用户交互对象
out 往页面输出对象
pageContext
error
exception

Struts2

  1. 拷贝jar
  2. 拷贝web.xml
  3. 拷贝struts.xml将package包中的action指向自己的类(继承actionSupport) allowed-methods
  4. 在action中的类编写各种符合struts规范的方法(1 返回值为string;2 没有
    参数;3 抛出异常;)
  5. 页面引入struts标签。<%@taglib prefix="s" uri="/struts-tags" %>
  6. <s:action name="teacherAction_getAllTeacher" executeResult="false" namespace="/"/>
    name中前半部分和struts-xx.xml中name一致,后半部分和action类的方法一致。

Struts查错

  1. 找到服务器的work和wtp
    workspace\.metadata\.plugins\org.eclipse.wst.server.core\temp0
  2. 放开方法 struts.xml中 <allowed-methods>getAllTeacher</allowed-methods>
  3. namespace对应(struts.xml&jsp)

Example

TeacherAction.java

package gxa.action;

import java.io.PrintWriter;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import gxa.entity.Teacher;

public class TeacherAction extends ActionSupport{
    //struts类中的方法必须符合以下要求,1 返回值为Sting 2不能有参数  3抛出异常
    private Teacher teacher;
    
    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
    
   //查询全部教师
    public String getAllTeacher() throws Exception{
        PrintWriter out = ServletActionContext.getResponse().getWriter();
        out.println("getAllTeacher");
        System.out.println("getAllTeacher");
        return null;
    } 
    //增加一个教师
    public String addTeacher(){
        System.out.println("add");
        return null;
    }

}

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">


<struts>
<include file="struts-teacher.xml"/>


   <!-- struts把url请求分模块处理,name随意,extends继承默认包 -->
    <package name="struts2" extends="struts-default" namespace="/"> 
    </package>
</struts>

struts-teacher.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">


<struts>
   <!-- struts把url请求分模块处理,name随意,extends继承默认包 -->
    <package name="struts2" extends="struts-default" namespace="/">
        <action name="teacherAction_*" class="gxa.action.TeacherAction" method="{1}">
                <allowed-methods>getAllTeacher,addTeacher</allowed-methods>
        </action>
        
    </package>
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Archetype Created Web Application</display-name>
  
  
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.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>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

list.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<s:action name="teacherAction_getAllTeacher" executeResult="false" namespace="/"/>
</body>
</html>

上一篇下一篇

猜你喜欢

热点阅读