原文来自搬砖工,如需转载请注明出处
博主之前公司使用的是第三方框架Jforum,如果大家有兴趣的可以去百度Jforum,它是一个开源论坛项目,使用的是自己独有的第三方框架,而且无版权使用问题。是一个比较优秀的第三方框架。之前把javase的有关知识系统得总结了一遍,但是还是有许多遗漏的知识点。博主打算在之后对javase的知识进行补充,先把三大开源框架温习一下。毕竟没有经常使用,还是有很多生疏的地方。
首先就从Struts2开始,接下来的SSH知识总结将作为一个系列重新学习一下SSH框架中常用的技术和开发中的细节问题。这篇文章就先总结一下搭建一个简单的Struts项目。
一、获取Struts2相关文件
1.下载Struts2压缩包struts-2.3.31-all.zip
-
把包下载完毕之后,解压进入文件夹 struts-2.3.31-all\struts-2.3.31\apps ,我们能看到有几个war包。把struts2-blank.war拷贝到tomcat的webapps目录下。
-
启动tomcat,我们看到war包解压成了文件夹。我们需要的就是lib包下面的jar文件,同时在写配置的时候可以参考web.xml的配置
二、创建一个Struts项目
使用myeclipse创建一个web项目,在创建的时候可以先next勾选创建一个web.xml的配置文件。如果直接finish也行,我们之后需要拷贝一个web.xml文件就行。(项目名称为Struts)
项目创建完毕之后,我们先把之前解压的项目中的lib文件拷贝到Struts的lib下面,刷新项目。(如果不是从myeclipse面板新增文件,我们需要刷新项目)然后我们把下面的配置写到web.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Struts</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
解释:
-
filter是过滤的意思,在javaweb中我们称之为过滤器。filter-name是给下面的类给个别名,在filter-mapping中使用的时候,filter-name的值要和上面定义的对应。
-
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter是struts2新的过滤器,老版本有一个另外的过滤器。大家在实际使用的时候可以去了解一下,当你使用旧的过滤器的话,struts2会提示建议使用新的过滤器。
这样就把最基本的web.xml文件配置好了,接下来在src中创建一个struts2的配置文件,名称为struts.xml。struts2与struts1不同的地方就是不需要在web.xml中配置struts的配置文件了。
然后我们建一个类LoginAction
package com.study.action;
public class LoginAction {
public String execute() throws Exception{
if(name!=null&&name.equals("admin")){
return "success";
}else{
return "input";
}
}
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
然后配置我们的struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="example" namespace="/" extends="struts-default">
<action name="login" class="com.study.action.LoginAction">
<result name="success">/Success.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
解释:
1.action的name属性是页面请求的时候的名称,class是我们类的所在位置。
2.result是类中的execute()类返回值所对应的,它标识所跳转的页面
简单的jsp示例:
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form id="login" name="login" action="/login.action" method="post">
<table border="0">
<tr>
<td>name:</td>
<td><input type="text" name="name" id="name"/></td>
</tr>
<tr>
<td>password:</td>
<td><input type="password" name="password" id="password"/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="login"/></td>
</tr>
</table>
</form>
</body>
</html>
Success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
hello world!
</body>
</html>
到此我们一个简单的struts2项目创建完毕
三、运行项目
打开我们的浏览器,请求项目的login.jsp。博主tomcat配置的方式有所不同
博主的请求域名就是
就能看到我们的输入框以及提交按钮。按照action中的逻辑,成功会显示helloword,失败会跳转回来。表单的请求action是/login.action,.action是struts2默认的后缀,意思就是在我们配置的login后面还需要跟上.action。
跳转成功页面
我们发现,域名以及改变成我们请求的action了
四、简单总结