struts2学习笔记之一、eclipse搭建自己的第一个web工程

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jQuerys/article/details/52348394

1、下载struts2的的jar的压缩包,下载地址:http://struts.apache.org/download.cgi#struts252

2、下载完后,解压力下载后的压缩包, 我们将看到,目录中有三个目录,分别是app目录(struts2的demo工程,可以放到服务器中直接运行)、docs目录(帮助文档)、lib目录(struts2的核心jar包和struts2所依赖的jar文件)、src目录(源码目录)

3、eclipse中新建web工程:struts2_first,并改变class文件的位置struts2_first\WebContent\WEB-INF\classes,通过右键工程Builder Path:如下图


4、将struts2必须的几个jar包从lib目录上选出来,copy到工程的lib目录下,如下图中:



4、为了将struts2引入到我们项目上,我们要修改web.xml文件,,struts是通过一个过滤器来融入到工程中的,将struts2的过滤器StrutsPrepareAndExecuteFilter引入到项目中,,以后,并配置/*,,意思,所以的组件,都会被这个过滤器过滤,

struts2就是通过,这个过滤器,来接收所有的请求,再通过 配置,来跳转到相应的action中的,StrutsPrepareAndExecuteFilter过滤器在struts2的核心包中,如下图:


将其配置到,web.xml中,代码如下:


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>struts2_01</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.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>


5、在工程的WebContent下新建一jsp的包,并建立一个测试的登录的jsp文件,名为login.jsp,并新建两个,请求成功后跳转 success.jsp和访问失败后跳转的error.jsp,,login.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">
<script type="text/javascript" src="../js/jquery-1.12.0.js"></script>
<title>Insert title here</title>
</head>
<body>
<form action="loginAction" method="post">
用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="password"/><br/>
生日<input type="text" name="birthday"/><br/>
<input type="submit" name="submit" value="submit"/>
</form>
</body>
</html>



success.jsp------->

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@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>Insert title here</title>
</head>
<body>
用户名:<s:property value="username"/>
密码:<s:property value="password"/><br/>
birthday:<s:property value="birthday"/><br/>
</body>
</html>


6、在工程的src目录下,建,com.kingkong.struts2.action包,并在包下建第一个Action,取名为LoginAction并继承ActionSupport:代码如下:


package com.kingkong.struts2.action;


import java.util.Date;


import com.opensymphony.xwork2.ActionSupport;


public class LoginAction extends ActionSupport {


/**

*/
private static final long serialVersionUID = 1L;

private String username;
private String password;
private Date birthday;
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;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}

@Override
public String execute() throws Exception {
return super.execute();
}


}


7、在src目录下,建一个struts2的xml配置文件,文件名固定,名为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="struts" extends="struts-default">
<action name="loginAction" class="com.kingkong.struts2.action.LoginAction" >
<result name="success">/jsp/success.jsp</result>
<result name="error">/jsp/error.jsp</result>
</action>
</package>
</struts>

8、在tomcat的server.xml部署项目,

<Context path="/struts2_first" docBase="D:\workspace\struts2_first\WebContent" reloadable="true"/>


9、eclipse中启动tomcat

10、在浏览器中输入测试:http://127.0.0.1:8080/struts2_first/jsp/login.jsp

如下图:



点击submit后:


猜你喜欢

转载自blog.csdn.net/jQuerys/article/details/52348394
今日推荐