maven+struts2

来源:互联网 发布:软件打包有哪些 编辑:程序博客网 时间:2024/04/29 22:17

一.what--why--how

1.什么是Struts2:

使用jsp/servlet的时候是将servlet,filter配置在web.xml中的,实际上struts2可以理解为一个filter(org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter),web容器将符合条件的请求交给这个filter进行处理,然后这个filter会在初始化的时候init方法中加载<init-param>  <param-name>filterConfig</param-name>  <param-value>classpath:struts.xml</param-value>   </init-param>这个配置文件,然后根据这个配置文件各个请求分别分配给不同的action去处理。

2.为什么需要struts2:

(1)在MVC模式下,它提供了强大的V层(可定制的标签库)和C层(控制器)功能;

(2)便于与其他技术框架整合。

3.怎么样使用struts2:

(1).pom.xml文件:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <struts2.version>2.3.20</struts2.version> 
  </properties>

<dependency>  
            <groupId>org.apache.struts</groupId>  
            <artifactId>struts2-core</artifactId>  
            <version>${struts2.version}</version> 
</dependency>

<dependency>  
            <groupId>org.apache.struts</groupId>  
            <artifactId>struts2-convention-plugin</artifactId>  
            <version>${struts2.version}</version>  
</dependency>

(2).配置struct.xml(文件可以随意命名,主要是在web.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>
    <constant name="struts.i18n.reload" value="true" />
    <constant name="struts.devMode" value="false" />
    <!-- <constant name="struts.action.extension" value="action" /> -->
    <include file="struts-default.xml" />
    <include file="struts-test.xml" />
</struts>

其中<include file="struts-test.xml" />可引入struts配置文件,这样便于模块化管理。

struts-test.xml的内容如下:

<struts>
    <package name="default" extends="struts-default" namespace="/test">
        <action name="login" class="com.iuniversal.action.HelloAction" method="login">
            <result name="success">../index.jsp</result><!--这里如果package标签的namespace="/",那么“../index.jsp修改为“index.jsp”-->
            <result name="login">../jsp/login.jsp</result>
        </action>
    </package>
</struts>

(3)配置web.xml文件:

    <filter>
        <filter-name>struts2</filter-name>  
        <filter-class>  
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
        <init-param>  
            <param-name>filterConfig</param-name>  
            <param-value>classpath:struts.xml</param-value>  
        </init-param>
    </filter>  
    <filter-mapping>  
        <filter-name>struts2</filter-name>  
        <url-pattern>/*</url-pattern>  
   </filter-mapping>

其中StrutsPrepareAndExecuteFilter的init方法需要传递的参数正是filterConfig;

(4)编写Action类:

public class HelloAction extends ActionSupport{private static final long serialVersionUID = 1L;        public String execute(){          return SUCCESS;      }          public String login() {          try {            HttpServletRequest request = ServletActionContext.getRequest();              HttpServletResponse response = ServletActionContext.getResponse();              request.setCharacterEncoding("UTF-8");              response.setContentType("text/html;charset=utf-8");              String username = request.getParameter("username");              String password = request.getParameter("password");               if ("user1".equals(username) && "123456".equals(password)) {                  return SUCCESS;              } else {                return "login";            }        } catch (UnsupportedEncodingException e) {              e.printStackTrace();          }          return SUCCESS;      }}
Jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"      pageEncoding="UTF-8"%>  <!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>登录界面</title>      </head>            <body> <form action="../test/login" method="post"> <table><tr><td>用户名:</td><td><input type="text" name="username" /> </td></tr><tr><td>密码:</td><td><input type="text" name="password" /> </td></tr><tr><td colspan="2"><input type="submit" value="登录" /><input type="reset" value="重置" /></td></tr></table></form>    </body>  </html> 

启动tomcat,即可访问项目。

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 手机显示系统空间不足怎么办 联系人存储已停止运行怎么办 荣耀note8听筒声音小怎么办 红米4x内存不足怎么办 安卓电视内存小怎么办 红米手机运行内存不足怎么办 红米手机存储空间不足怎么办 华为手机储存空间不足怎么办 大麦机顶盒遥控器丢了怎么办 大麦盒子总自动加软件怎么办 小米手机玩王者荣耀卡怎么办 荣耀8青春版信号差怎么办 华为mate8电池不耐用怎么办 华为4x内存小怎么办 发现手机被用过怎么办 华为p8手机声音小怎么办 手机指纹解锁密码忘了怎么办 华为p10黑色掉漆怎么办 金立手机导航信号弱怎么办 手机导航时gps信号弱怎么办 三星手机导航gps信号弱怎么办 小米5导航信号弱怎么办 华为手机导航gps信号弱怎么办 手机屏玻璃裂了怎么办 oppo音量键坏了怎么办 魅蓝手机屏幕碎了怎么办 手机钢化膜边缘有气泡怎么办 手机贴钢化膜边缘有气泡怎么办 苹果手机钢化膜边缘有气泡怎么办 手机膜里的水泡怎么办 膜贴好了有气泡怎么办 后档玻璃膜气泡怎么办 贴膜里面有灰尘怎么办 贴膜边缘不粘怎么办 贴膜时候的气泡怎么办 贴手机膜起泡了怎么办 手机膜起泡泡了怎么办 手机膜鼓起来了怎么办 贴膜中间有气泡怎么办 新车贴膜有气泡怎么办 贴手机膜进灰尘怎么办