简单Struts2框架搭建

来源:互联网 发布:网络的利与弊素材 编辑:程序博客网 时间:2024/06/05 07:26

Struts2简介

什么是Struts2
1.Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet
2.Struts 2以WebWork为核心,采用拦截器的机制来处理用户的请求,这样的设计也使得业务逻辑控制器能够与ServletAPI完全脱离开,所以Struts 2可以理解为WebWork的更新产品
Struts2的优点
1.Struts2的应用不依赖于ServletAPI和struts API
2.无侵入式设计
3.支持多种表现层


以下搭建一个基础的Struts2框架

导入相关jar包

链接:http://pan.baidu.com/s/1jIf4MsI 密码:jtt1

配置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/j2ee" xmlns:javaee="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/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_9" version="2.4">   <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>  <welcome-file-list>    <welcome-file>hello.jsp</welcome-file>  </welcome-file-list></web-app>

编写一个jsp页面

<%@ taglib prefix="s" uri="/struts-tags" %>
<body>        <div>            <h1><s:property value="message"/></h1>        </div>        <div>            <form action="hello" method="post">                <input type="text" name="name">                <input type="submit" name="提交">            </form>        </div>  </body>

编写开发控制层Action类

package cn.action;import com.opensymphony.xwork2.Action;public class helloAction implements Action{    private String name="";    private String message="";    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getMessage() {        return message;    }    public void setMessage(String message) {        this.message = message;    }    @Override    public String execute() throws Exception {        // TODO Auto-generated method stub        //success是Struts2自带的返回值        return "success";    }}

编写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>        <package name="default" namespace="/" extends="struts-default">            <!-- 配置控制器  处理器  处理结果和响应视图的映射 -->            <action name="hello" class="cn.action.helloAction">                <result name="success">hello.jsp</result>            </action>        </package></struts>

基本配置
这里写图片描述

原创粉丝点击