1、首先我们找到自己的tomcat,找到里面的webapps目录。
2、在这里创建一个文件夹取名为struts2
3、创建一个如图目录,一个WEB-INF文件夹,一个META-INF文件夹,2个jsp页面,一个loginForm.jsp,一个show.jsp
如图:
注意:这里的META-INF没有好像是跳转不了的,后面详细介绍.
4.文件内容如下;
META-INF中有个MANIFEST.MF文件,内容为Manifest-Version: 1.0Class-Path:
WEB-INF目录中有classes文件夹,lib文件夹和web.xml
5、classes里面放你的action类的编译后的class以及struts.xml,当然如果有包名得放在嵌套目录中(就是在classes里创一个com文件夹(和struts.xml同目录),com下再创个lin,lin下再创个action,action下放.class)
6.lib里面放入struts2的jar包
下面给各文件的代码
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>loginForm.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>
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!--
/*
* $Id: struts-default.xml 1485719 2013-05-23 14:12:24Z lukaszlenart $
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
-->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="loginForm" extends="struts-default" namespace="/login">
<action name="login" class="com.lin.action.LoginAction">
<result name="success">
/show.jsp
</result>
</action>
</package>
</struts>
LoginAction.java
package com.lin.action;
public class LoginAction{
private String username;
private String password;
public void setUsername(String username){
this.username=username;
}
public String getUsername(){
return this.username;
}
public void setPassword(String password){
this.password=password;
}
public String getPassword(){
return this.password;
}
public String execute(){
return "success";
}
}
loginForm.jsp:
<%@page contentType="text/html; charset=GBK" language="java" errorPage=""%>
<!DOCTYPE html>
<html>
<head><title>登陆页面</title></head>
<body>
<form action="login/login.action" >
用户名:<input type="text" name="username"/>
密码:<input type="password" name="password"/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
show.jsp:
<%@page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<h1>恭喜你,登录成功</h1>
<s:property value = "username"/><br/>
<s:property value = "password"/><br/>
</body>
</html>
在这里主要注意loginForm那个jsp页面form表单对应的action跳转路径是login/login.action,前面的login表示包的namespace,如果没有META-INF文件的话,每次提交后,路径会丢失掉struts2这个项目路径,导致跳转action失败。
浏览器输入http://localhost:8080/struts2/
进入首页,在web.xml配置了首页是loginForm那个jsp页面了,所以url这么写就可以了。