关闭

maven 创建struts2简单项目

标签: strutsmaven
178人阅读 评论(0) 收藏 举报

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8368次
    • 积分:254
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:5篇
    • 译文:3篇
    • 评论:0条
    文章分类