上周开始接触java web,打算这周末抽时间搞一下struts2在eclipse 中基本的配置 ,本以为很简单,结果发现全是坑啊,简直不能忍
本来之前买了一本《轻量级Java EE企业应用实战-struts 2 + Spring +Hibernate》,里面有配置步骤,等到在Mac中搞时候,可能由于struts 版本问题按照上面的步骤一步一步来,发现一直404,简直就要奔溃了,搞得我不得不在网上查遇到的每一个问题,幸好还是可以找得到答案的。尽管如此,还是找了很久,服务器运行了很多次,依然404,好在就在我快要放弃时候突然找到了一个博客,里面正是我要的答案:http://blog.csdn.net/lyue4/article/details/71192651 按着上面的步骤终于可以配置成功了。
对一个java web的小白来说,每一步的小问题,可能要花很多时间去解决,这真是一个很蛋疼的问题。为了以防下次忘记步骤,只好纪录一下,防止从头再来。
第一步:
安装eclipse
https://www.eclipse.org/downloads/
第二步:
安装Tomcat
http://tomcat.apache.org
第三步:
下载需要的struts2包
http://struts.apache.org/download.cgi#struts25101
由于只是做一个测试,所以我下载的是最小的那个包

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

如果需要解压.war包,在mac中不知道怎么解压.war包,就用了命令行:
jar -xvf xxxxxxx.war
第五步:
创建一个工程,添加相应文件
层次结构如下图

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

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"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<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" "http://www.w3.org/TR/html4/loose.dtd">
<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" "http://www.w3.org/TR/html4/loose.dtd">
<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" "http://www.w3.org/TR/html4/loose.dtd">
<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;
}
}
第七步:
-
测试服务器是否可用
网址输入:http://localhost:8080/Struts2Test/index.jsp
B0B75838-BDC5-4F44-AE19-2E3D467E4262.png
-
随便输入、成功之后跳转
E3EC3541-2A7E-457D-BA41-195CA3CA3E81.png
-
如果直接访问:http://localhost:8080/Struts2Test/login
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();
}

- 输入一个buyi__

- 什么都不输提交

网友评论