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

初识Struts2(如何建构一个简单的项目)

2017-03-04 19:36 363 查看

开发流程:

 1、准备Jar包(到Blank项目中拷)

 2、将struts2的核心类配置到web.xml -------配过滤器(filter): org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

 3、写一个控制器(Action)--EJB=JavaBean+业务方法(默认String execute() )

 4、写返回页面(JSP页面)

 5、写struts2的配置文件:struts.xml---把我们写的Action和返回页面配置到该文件中

 6、测试


最小包:



Struts2能帮我们做:

1、帮助我们把前端页面上传的参数获取,并封装成EJB(Action对象)

2、帮助我们自动把信息存放到request中

3、帮我们实现页面转发(根据struts.xml中的结果页面配置 <action>中的<result>标签配置)

4、以上3个是核心功能。另外,还有国际化、表单可配置、文件上传下载、页面标签库,验证框架,对Ajax的支持,数据库连接和事务,拦截器!

 

canStrutsHello



 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd"> <display-name></display-name>

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

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>

<body>
<!-- 表单请求地址为struts.xml配置文件中action的name值 -->
<form action="aa/login.action" method="post">
姓名:<input type="text" name="userName" /><br/>
密码:<input type="text" name="password" /><br/>
<input type="submit" value="登录" />
</form>
</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值可随意取,可被继承,namespace属性值"/"+字符,可防止action的name值相同时出错,进行分辨。 extends属性 值struts-default继承struts的一个默认包-->
<package name="login" namespace="/aa" extends="struts-default">
<action name="login" class="cn.hncu.user.login.LoginAction">
<result name="index">/index.jsp</result>
<result name="success">/jsps/success.jsp</result>
</action>
</package>
</struts>


LoginAction.java

package cn.hncu.user.login;

import com.opensymphony.xwork2.ActionSupport;

/*
* 注意:
* 1.写Action时一定要要写  set  get  方法,在业务方法中可以直接属性名或  get方法直接获取属性值
* 2.写Action时,属性名要和前台发送数据的属性名一致,这样struts会帮我自动获取并且封装到Action中
* 3.写Action时,业务方法的返回值是一个字符串,该值决定导向不同结果对象页面(字符串值与页面的对应关系,由struts.xml的<action>来定义)
* 4.写Action时,如果想使用request,response,session,application等,继承com.opensymphony.xwork2.ActionSupport类
*/
public class LoginAction extends ActionSupport{

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//业务逻辑方法------调用service层
public String execute() throws Exception {
/*
//这一段展示使用web容器------普通EJB也可以这样使用
ActionContext context=ActionContext.getContext();
context.getApplication().put("user", "Tom");//类似  application.setAttribute("user","Tom");
String user=(String) context.getApplication().get("user");//application.getAttribute("user");
System.out.println("user :"+user);
*/

//		String name=getUserName();
//		String pwd=getPassword();
String name=userName;
String pwd=password;
//按理这里应该访问service层以进行登录判断,此处模拟了
System.out.println("这里模拟登录service了.......");
boolean boo=name.endsWith("hncu")&&pwd.equals("1234");
if(boo){
return "success";
}else{
return "index";
}
}
@Override
public void validate() {
System.out.println("校验:"+userName+","+password);
}

}


success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>

<body>
<h2>恭喜你,登录成功了!</h2>
</body>
</html>


 

主页:



 

登陆结果:

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