maven 创建struts2简单项目

1.项目目录:

2.创建userAction:

package action;


import java.io.UnsupportedEncodingException;


import org.apache.struts2.ServletActionContext;


import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  


import com.opensymphony.xwork2.ActionSupport;


public class UserAction 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");  
            System.out.println("name->" + username + ",password->"  
                    + password);  
            if ("admin".equals(username) && "123456".equals(password)) {  
                return SUCCESS;  
            } else {  
                return "login";  
            }  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }  
        return SUCCESS;  
    }  
}  


3.创建resources源目录,创建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>  
  
    <constant name="struts.i18n.reload" value="false" />  
    <constant name="struts.devMode" value="false" />  
     
  
    <package name="default" extends="struts-default" namespace="/">  
        <action name="login" class="action.UserAction" method="login">  
            <result name="success">index.jsp</result>  
            <result name="login">login.jsp</result>  
        </action>  
    </package>  
  
</struts>  


4.在src目录下新建folder(webapp,WEB-INF)

在WEB-INF下创建web.xml

<!DOCTYPE web-app PUBLIC  
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  
 "http://java.sun.com/dtd/web-app_2_3.dtd" >  
  
<web-app>  
  <display-name>Archetype Created Web Application</display-name>  
      
    <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>login.jsp</welcome-file>  
    </welcome-file-list>  
</web-app>  


5.创建index.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>Hello Maven</title>    
    </head>    
        
    <body>    
        <p>Hello world</p>    
    </body>    
</html>  


6.创建login.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="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>    



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值