Struts 2框架创建的第一个项目
生活随笔
收集整理的這篇文章主要介紹了
Struts 2框架创建的第一个项目
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
創(chuàng)建我的第一個Struts 2項(xiàng)目。
用Struts 2框架創(chuàng)建一個簡單的WEB項(xiàng)目。
效果演示
登錄頁面
登陸成功之后顯示的頁面
在程序正式開始之前注意引入架包(注意:架包放在WEB-INF下面的lib包里)
Struts 2一共有13個架包如下圖所示
在開始我們的代碼之前看一下目錄結(jié)構(gòu)
代碼演示
1.UserAction.java
package com.hnpi.action;public class UserAction {private String name;private String pwd;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}public String login(){System.out.println(name+":"+pwd);return "success";}}2.LoginServlet.java
package com.hnpi.servlet;import java.io.IOException; import java.io.PrintWriter;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public class LoginServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");String name = request.getParameter("name");String pwd = request.getParameter("pwd");System.out.println(name+":"+pwd);PrintWriter out = response.getWriter();out.print("success");}}3.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="test" extends="struts-default"><action name="login" class="com.hnpi.action.UserAction" method="login"><result name="success">/success.jsp</result></action></package> </struts>4.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.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>5.index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><form action="login" method="post">賬號:<input type="text" name="name"><br>密碼:<input type="text" name="pwd"><br><input type="submit" value="登錄"></form></body> </html>6.success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>My JSP 'success.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body>登錄成功之后看到的頁面!!!</body> </html>我的第一個Struts 2框架項(xiàng)目就做好了,你也趕快去試一下吧!!!
總結(jié)
以上是生活随笔為你收集整理的Struts 2框架创建的第一个项目的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。