热门搜索 :
考研考公
您的当前位置:首页正文

java struts2 在eclipse中的配置

来源:东饰资讯网

上周开始接触java web,打算这周末抽时间搞一下struts2在eclipse 中基本的配置 ,本以为很简单,结果发现全是坑啊,简直不能忍

对一个java web的小白来说,每一步的小问题,可能要花很多时间去解决,这真是一个很蛋疼的问题。为了以防下次忘记步骤,只好纪录一下,防止从头再来。

第一步:

第二步:

第三步:

由于只是做一个测试,所以我下载的是最小的那个包


DA402555-7FC7-4E41-9AA1-C2E63831E811.png

第四步:

下载完之后,在lib文件夹中有几个jar包

7D90839C-C012-4822-BC4F-699BFBA2468F.png

如果需要解压.war包,在mac中不知道怎么解压.war包,就用了命令行:
jar -xvf xxxxxxx.war

第五步:

创建一个工程,添加相应文件
层次结构如下图


BFF49E5C-CE1B-4532-A408-3CAE269F8C60.png

1、在src路径下创建一个com.Action包,里面添加一个test.java 文件
2、在WEB-INF 路径下添加一个添加一个lib文件夹,将步骤三种的lib下的.jar包添加进来,此时libraries中多出如下几个jar包


0FDC1DBE-5CB4-4044-B25C-DEC23EFC5D43.png

3、在src/com.Action 路径下添加一个strtus.xml 文件
4、在WebContent/WEB-INF路径下添加一个web.xml文件

由于本测试做一个登录跳转界面,所以添加相应额外的几个.jsp页面
5、在WebContent路径下添加一个hello.jsp,index.jsp,error.jsp页面

第六步:

配置相应文件
1、配置struts.xml文件

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
<!-- START SNIPPET: xworkSample -->
<struts> 
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="true" />
  
  <package name="struts2" namespace = "/" extends="struts-default">//此处的package name 为 struts2
  
  <action name ="login" class = "com.Action.test"> // 此处的class为映射的.java文件、包含前面的package名字
     <result name = "success">hello.jsp</result> // 返回成功、映射到hello.jsp 页面
     <result name = "error">error.jsp</result>       // 失败映射到 error.jsp 页面
  </action>
  </package>  
</struts>

2、配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" 
     
     
     

  
    <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>


</web-app>

3、配置index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>index.jsp</title>
</head>
<body>
<form action="login" method="post">
请输入用户名:<input type="text" name="userName">
<input type="submit" value="提交">
</form>
</body>
</html>

4、配置hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="e"%>  
<%@ taglib prefix="s" uri="/struts-tags" %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Success.jsp</title>
</head>
<body>

<h1>hello</h1>

<e:property value="userName"/>,您好

<br>
欢迎来到本站
</body>
</html>

5、配置error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
error page!
</body>
</html>

6、配置test.java 文件

package com.Action;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class test extends ActionSupport {

    public String userName;// 此处要用public 不然index.jsp 传入的数据无法set进来
    
    public String execute() throws Exception {
    
        System.out.print(userName);
        if(userName == null || "".equals(userName)){
            return ERROR;
        }else{
            return SUCCESS;
        }
            
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}

第七步:

  • B0B75838-BDC5-4F44-AE19-2E3D467E4262.png
  • 随便输入、成功之后跳转


    E3EC3541-2A7E-457D-BA41-195CA3CA3E81.png
  • DECC02F5-7F9B-40A7-96A2-E335167AFF29.png
    直接转到error.jsp页面

第八步:

在xcode上用模拟器联一下试试
创建一个工程、然后用一个UIWebView来加载一下上面的那个jsp页面
在控制器中随便写写

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *button=   [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 40)];
    button.backgroundColor = [UIColor yellowColor];
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    self.view.backgroundColor = [UIColor whiteColor];
    __weak typeof(self) weakSelf = self;
    self.block = ^{
        weakSelf.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 200, 420, 200)];
        [weakSelf.view addSubview:weakSelf.webView];
        
        NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.2.101:8080/Struts2Test/index.jsp"]];
        
//        [req setValue:@"fei_headerField" forHTTPHeaderField:@"fei"];
        
//        [req setValue:@"text/json" forHTTPHeaderField:@"Content-Type"];

//        [req setHTTPMethod:@"POST"];
        [weakSelf.webView loadRequest:req];
        
    };
}
- (void)buttonClick {
    self.block();
}

B29EF13D-B72C-451F-9756-B16E443F29FF.png
  • 输入一个buyi__
D6EA719C-66ED-4E83-9C50-DC3AC2996337.png
  • 什么都不输提交
8C7D2191-5E5F-4768-8AF1-6E287B97CCA4.png
Top