这是我写的struts2开发的登陆小项目简单版本还有一个增强版需要的可以看我的博客(struts开发增强版)适合初学者
开发环境:eclipse+jdk1.7+tomcat7+utf-8
下面是我的结构图
接下来是jar包
开始写代码:index.jsp代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- 引入struts的标签库-->
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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>登录</title>
</head>
<body>
<form action="index">
用户名:<input type="text" name="username"><br>
密 码:<input type="password" name="password"><br>
<input type="submit" value="ok">
</form>
</body>
</html>
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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Demo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<!-- 配置Struts2核心Filter的名字 -->
<filter-name>struts2</filter-name>
<!-- 配置Struts2核心Filter的实现类 -->
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- 配置Filter拦截的URL -->
<filter-mapping>
<!-- 配置Struts2的核心FilterDispatcher拦截所有用户请求 -->
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
WebContent/jsp文件下有success.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>
this is login.jsp
</body>
</html>
struts.xml代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 定义逻辑视图与物理视图之间的联系 -->
<package name="trio" extends="struts-default" namespace="/">
<action name="index" class="com.struts.trio.HelloStrutsAction">
<result name="success">/jsp/success.jsp</result>
<result name="failure">/jsp/failure.jsp</result>
</action>
</package>
<!-- end -->
</struts>
HelloStrutsAction代码
package com.struts.trio;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class HelloStrutsAction extends ActionSupport {
//定义请求参数的 username 和password
//与前台jsp中表单中的name属性相同
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
String userName=this.getUsername();
String userPassword=this.getPassword();
if(userName.equals("admin")&&userPassword.equals("123456")){
ActionContext.getContext().getSession()
.put("user", userName); //用户名存储在 session 用于返回界面显示
return "success";
}
return "failure";
}
}
按以上步骤只要jar包正确基本无问题(已测试过)