struts2 基础入门

来源:互联网 发布:生物科学类 知乎 编辑:程序博客网 时间:2024/05/17 16:54
strtus2搭建步骤:

1.拷贝所需jar到WEB工程目录下的lib


2.配置WEB.xml文件,配置过滤器Filter
<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>
3.编写login.jsp:
<form action="login.action" method="post">
<h1>用户登录</h1>
账号:<input type="text" name="userName"/><br/>
密码:<input type="text" name="password"><br/>
<input type="submit" value="登录"/>
</form>

4.创建LoginAction类继承ActionSupport类,重写execute()

package com.learn.action;


import com.opensymphony.xwork2.ActionSupport;


public class LoginAction extends ActionSupport{
private String userName;
private String password;

@Override
public String execute() throws Exception {
System.out.println("账号:"+userName);
System.out.println("密码:"+password);

return "success";
}
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;
}


}

5.拷贝struts.xml配置文件到src目录下:
<?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>
<!-- 动态方法调用 -->
   <constant name="struts.enable.DynamicMethodInvocation" value="false" />
   <!-- 开发模式 -->
   <constant name="struts.devMode" value="true" />

   <package name="default" namespace="/" extends="struts-default">
<action name="login" class="com.learn.action.LoginAction">
<result name="success">/success.jsp</result>
</action>
   </package>
</struts>     

6、编写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>
<h1>用户登陆成功</h1>
账号:${userName}<br>
密码:${password}<br>
</body>
</html>

0 0
原创粉丝点击