maven构建简单struts2示例

1.添加maven依赖:在 pom.xml 中添加struts-core的依赖

添加位置在 <dependencies> 与 <!-- log start --> 之间

    <!-- junit -->  
    <dependency>  
      <groupId>junit</groupId>  
      <artifactId>junit</artifactId>  
      <version>4.9</version>  
      <scope>test</scope>  
    </dependency>  
      
    <!-- struts2依赖包 -->  
    <dependency>  
        <groupId>org.apache.struts</groupId>  
        <artifactId>struts2-core</artifactId>  
        <version>2.3.14</version>  
    </dependency> 

添加完成后保存会检查本地库有无包,没有会自动下载。

若下载失败则会报错,此时需要去提示的路径删除文件夹或更新maven项目。

 

2.新建一个Action:在src/main/java 下新建 UserAction.java

 1 package com.stu2.action;
 2 
 3 import java.io.UnsupportedEncodingException;
 4 
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletResponse;
 7 
 8 import org.apache.struts2.ServletActionContext;
 9 
10 import com.opensymphony.xwork2.ActionSupport;
11 
12 public class UserAction extends ActionSupport {
13 
14     private static final long serialVersionUID = 1L;
15 
16     public String execute(){
17         return SUCCESS;
18     }
19     
20     public String login(){
21         
22         try {
23             HttpServletRequest request = ServletActionContext.getRequest();
24             HttpServletResponse response = ServletActionContext.getResponse();
25             
26             // 常见的两行确保为utf-8格式的代码(如果有过滤器则不需要)
27             request.setCharacterEncoding("UTF-8");
28             response.setContentType("text/html;charset=utf-8");
29             
30             String username = request.getParameter("username");
31             String password = request.getParameter("password");
32             System.out.println("name->"+username+",password->"+password);
33             
34             if("admin".equals(username) && "123456".equals(password)){
35                 return ERROR;
36             }else{
37                 return "login";
38             }
39             
40         } catch (UnsupportedEncodingException e) {
41             e.printStackTrace();
42         }
43         return ERROR;
44     }
45 
46 }

 

3.配置 Struts2.xml :一般放在类路径下,如果不在类路径则需要在 web.xml 中配置路径(步骤4)

 1 <?xml version="1.0" encoding="UTF-8" ?>  
 2   
 3 <!DOCTYPE struts PUBLIC  
 4     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
 5     "http://struts.apache.org/dtds/struts-2.3.dtd">  
 6   
 7 <struts>  
 8   
 9     <constant name="struts.i18n.reload" value="false" />  
10     <constant name="struts.devMode" value="false" />  
11      
12     <include file="struts-default.xml" />  
13   
14     <package name="default" extends="struts-default" namespace="/">  
15   
16         <action name="login" class="com.stu2.action.UserAction" method="login">  
17             <result name="success">index.jsp</result>  
18             <result name="login">login.jsp</result>  
19             <result name="error">error.jsp</result>
20         </action>  
21   
22     </package>  
23   
24 </struts>  

注意其中类的路径

 

4.配置 web.xml 监听器等

 1 <!DOCTYPE web-app PUBLIC  
 2  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  
 3  "http://java.sun.com/dtd/web-app_2_3.dtd" >  
 4   
 5 <web-app>  
 6   <display-name>Archetype Created Web Application</display-name>  
 7     <init-param>  
 8         <param-name>config</param-name>  
 9         <param-value>../../resources/struts.xml</param-value>  
10     </init-param>  
11       
12     <filter>  
13         <filter-name>struts2</filter-name>  
14         <filter-class>  
15             org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
16         </filter-class>  
17     </filter>  
18     <filter-mapping>  
19         <filter-name>struts2</filter-name>  
20         <url-pattern>/*</url-pattern>  
21     </filter-mapping>  
22   
23     <welcome-file-list>  
24         <welcome-file>login.jsp</welcome-file>  
25     </welcome-file-list>  
26 </web-app>  

 

5.建立页面 login.jsp index.jsp error.jsp

 1 <!--  login.jsp  -->
 2   
 3 <%@ page language="java" contentType="text/html; charset=UTF-8"    
 4     pageEncoding="UTF-8"%>    
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">    
 6 <html>    
 7     <head>    
 8         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    
 9         <title>登录界面</title>    
10     </head>    
11         
12     <body>   
13 <form action="login" method="post">   
14 <table>  
15 <tr>  
16 <td>用户名:</td>  
17 <td><input type="text" name="username" /> </td>  
18 </tr>  
19 <tr>  
20 <td>密码:</td>  
21 <td><input type="text" name="password" /> </td>  
22 </tr>  
23 <tr>  
24 <td colspan="2">  
25 <input type="submit" value="登录" />  
26 <input type="reset" value="重置" /></td>  
27 </tr>  
28 </table>  
29 </form>  
30     </body>    
31 </html>    
 1 <!--  index.jsp  -->
 2   
 3 <%@ page language="java" contentType="text/html; charset=UTF-8"    
 4     pageEncoding="UTF-8"%>    
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">    
 6 <html>    
 7     <head>    
 8         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    
 9         <title>登录成功</title>    
10     </head>    
11         
12     <body>    
13         <p>进来了</p>    
14     </body>    
15 </html>    
<!--  error.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>    
        <p>没进来</p>    
    </body>    
</html>   

 

6.注意编码问题  此文档代码为UTF-8和ISO混杂  懒得改了

posted @ 2018-01-30 15:52  大概是个小学生  阅读(1213)  评论(0编辑  收藏  举报