maven+struts2

一.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,即可访问项目。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值