最近在学习ssh框架,刚搞定struts项目的创建,分享出来,提供给大家参考。
首先在Eclipse中创建一个Dynamic Web Project,本文项目名称随意。下载好struts2,这个可以直接在apach下载。(http://apache.org/)
然后把把Struts内apps文件夹下的struts2-blank war包用压缩工具解压(可以在控制台用 jar xvf blank.war 指令进行解压),然后把解压出来的文件夹中得WEB-INF/lib内的jar包和WEB-INF下的web.xml文件分别复制到项目下的WEB-INF/lib内和WEB-INF下。
在src目录下创建相应package。
作为测试,我就建了2个jsp文件,进行测试。
web.xml
如果把整个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">
<display-name>Struts Blank</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>
index.jsp
提交一个表单,用来测试action
<%@page contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<form action = "dh12" method="post">
<input type="text" name="inputName"> <input type="submit" value="提交">
</form>
</body>
</html>
success.jsp
成功就跳转到这个页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String pate = request.getContextPath();
%>
<!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">
</head>
<body>
欢迎登陆
</body>
</html>
ActionDemo
package com.lizhi.action;
public class ActionDemo01 {
private String inputName;
public String getInputName() {
return inputName;
}
public void setInputName(String inputName) {
this.inputName = inputName;
}
public String execute(){
System.out.println("提交的内容:"+inputName);
return "success";
}
}
代码很简单,重在测试。