1. Eclipse工程图
2. 下载Struts2
地址:http://labs.xiaonei.com/apache-mirror/struts/binaries/struts-2.1.6-all.zip
解压后将struts-2.1.6\apps\struts2-blank-2.1.6\WEB-INF\lib目录下所有的jar加进工程的classpath
其实,根据官方文档,依赖的jar应该是
commons-fileupload, commons-io, commons-loggin, freemarker, ongl, struts2-core, xwork
3. Source
cn.edu.dlut.ssdut.pojo.User.java
package cn.edu.dlut.ssdut.pojo;
public class User {
private String id;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
cn.edu.dlut.ssdut.action.LoginAction.java
package cn.edu.dlut.ssdut.action;
import com.opensymphony.xwork2.ActionSupport;
import cn.edu.dlut.ssdut.pojo.User;
public class LoginAction extends ActionSupport {
private User user;
public void setUser(User user) {
this.user = user;
}
public User getUser() {
return user;
}
@Override
public String execute() throws Exception {
if (!isValid(user.getId()) || !isValid(user.getPassword())) {
return INPUT;
}
return SUCCESS;
}
private boolean isValid(String str) {
if (str != null && !"".equals(str.trim())) {
return true;
}
return false;
}
}
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<title>
Login Page
</title>
<head>
<title>Login Page</title>
</head>
<body>
<s:form action="hello/login" method="post">
<s:textfield name="user.id" label="UserId"/>
<s:password name="user.password" label="Password"/>
<s:submit value="Login"/>
</s:form>
</body>
</html>
home.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<title>Login Page</title>
</head>
<body>
Hello, <s:property value="user.id" />
</body>
</html>
以下是配置文件web.xml和struts.xml
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">
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default" namespace="/hello">
<action name="login" class="cn.edu.dlut.ssdut.action.LoginAction">
<result name="success">/home.jsp</result>
<result name="input">/index.jsp</result>
</action>
<!-- Add your actions here -->
</package>
</struts>
4. 编译及运行
分享到:
相关推荐
可以参照我的博客http://blog.csdn.net/xiaoqiangpku725/archive/2009/09/10/4538088.aspx
<action path="/login" name="LoginForm" type="com.yza.struts.action.LoginAction" scope="request" input="/index.jsp"> <forward name="success" path="/success.jsp"></forward> ...
struts2实现的学生信息管理系统 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" ...
本资源主要使用struts2实现实现后台服务,demo很简单,只含有struts2的代码,另两个框架,对于学过写ssh的很容易就能拼凑进去。url的拼凑:localhost:8080/MobileBackService/Login?jsonDate={"date": {"loginName":...
实现JSON类型的Result,连接oradle数据库,运用hibernate和struts2在实现效果,适合新手操作
打开IE,在地址栏中输入:http://localhost:8080/SSHChapter9Demo/login.jsp 7.如果一切正常,你会看到登录画面 8.如果点击"submit"按钮,会从数据库捞出数据显示在index.jsp中 9.如果点击其他两个按钮,是演示...
本DEMO实现登录、注册以及修改信息功能,仅供...(1) Struts2.1.8 (2) Spring2.0.2 (3) Hibernate3.2.5 注:个人开发环境不同,对于项目导入后可能出现的问题,请自行百度解决,多为jar包问题,请根据实际情况自行增减
本章我们将会开发基于Struts2框架的Hello World.我们的Hello World程序是你开发基于Struts2框架程序的第一步.这里我们会提供循序渐进的教程来开发基于Struts2框架的Hello World程序. 教程包含基本的步骤像创建目录...
│ struts.xml │ ├─28个java常用的工具类 │ │ Base64.java │ │ Base64DecodingException.java │ │ CConst.java │ │ CharTools.java │ │ ConfigHelper.java │ │ Counter.java │ │ CTool.java │ │...