美文网首页
Struts2、Hibernate、Spring三大框架整合员工

Struts2、Hibernate、Spring三大框架整合员工

作者: 年轻人Moriarty | 来源:发表于2017-03-02 09:34 被阅读0次

导入jar包.png

初始的配置文件:

  • 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>_SSH_ygglxt</display-name>

    <!-- Spring框架核心监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!-- Struts2框架核心过滤器 -->
    <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>
  • applicationContext.xml(spring中的bean.xml)
<?xml version="1.0" encoding="utf-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

</beans>
  • 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>
    <!-- 开发模式 (正式运行要改为false) -->
    <constant name="struts.devMode" value="true"></constant>
    <!-- 链接中感叹号DMI动态方法调用 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
</struts>
还有log4j.propertext.png

创建包结构

QQ截图20170302094708.png
package domain;

public class Product {
    private int pid;
    private String pname;
    private Double price;

    public int getPid() {
        return pid;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }
}

保存商品页面addProduct.jsp

addProduct.jsp.png
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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>
    <h1>保存商品页面</h1>
    <s:form action="" method="post" namespace="/" theme="simple">
        <table border="1" width="400">
            <tr>
                <td>商品名称</td>
                <td><s:textfield name="pname" /></td>
            </tr>
            <tr>
                <td>商品价格</td>
                <td><s:textfield name="price" /></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="添加" /></td>
            </tr>
        </table>
    </s:form>
</body>
</html>


相关文章

网友评论

      本文标题:Struts2、Hibernate、Spring三大框架整合员工

      本文链接:https://www.haomeiwen.com/subject/zxrhgttx.html