您的位置:首页 > 编程语言 > Java开发

(学习笔记)Struts2.3.3 入门与配置(一)

2016-09-23 22:16 423 查看
第一步:下载struts2.3.3  http://supergsego.com/apache/struts/2.3.30/struts-2.3.30-all.zip
第二步:新建一个WEB项目,导入以下9个jar架包



第三步:修改web.xml文件,配置struts2过滤器

<?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">

<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>

第四步:新建一个index.jsp页面,一个简单额表单提交页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>struts2</title>
</head>
<body>
<form action="Login.action" method="post">
username:<input type="text" name="username" /><br>
password:<input type="password" name="password"/><br>
<input type="submit" value="submit">
</form>
</body>
</html>

第五步:在src目录新建一个包com.action,然后在包中新建一个类LoginAction

package com.action;

/**
* index.jsp form表单处理
* **/
public class LoginAction
{
private String username;//必须和表单中的name属性一致
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;
}
//这个方法必须要,处理返回最终结果,和配置文件result name="success" 对应
public String execute()
{
return "success";
}
}


第六步:在src目录中新建一个struts.xml文件,配置请求,action name="Login" 为 index.jsp 中 action的访问地址,后面跟.action
<?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>
<!-- name随便填 必须继承struts-default namespace命名空间,说白了就是文件夹 -->
<package name="struts2" extends="struts-default" namespace="/">
<!-- action 一次请求, name为请求路径,这里是指index.jsp中form action的路径 -->
<!-- class 为处理请求的具体实现类 -->
<action name="Login" class="com.action.LoginAction">
<!-- result 根据处理结果,转跳到相关页面 -->
<result name="success">/result.jsp</result>
</action>
</package>
</struts>

第七步:新建一个result.jsp文件,显示提交的内容
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>result</title>
</head>
<body>
${requestScope.username}<br>
${requestScope.password}
</body>
</html>


最终结果如下:



最后,部署到tomcat 运行



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struts2.0 java J2EE WEB JSP