IDEA配置Struts框架
對于剛接觸編程的同學,對框架只是還不是很了解,本文主要介紹在Idea上配置Struts,實現簡單的頁面跳轉,以及頁面參數傳遞。
在進行代碼編寫之前先對Idea進行一個簡單了解,對于長時間接觸編程的,對于Eclipse或者MyEclipse并不陌生,想當初剛接觸編程的時候配置Eclipse運行環境花費了大量的時間,但作為一個程序員來說,我還是建議大家盡量運用Idea,舉一個簡單的例子,在Idea中進行Debug,那是很簡單的,只需要在代碼進行點擊,就可以直接進入Debug狀態,對于整個進程的參數都可以看得到,一目了然,這只是一個簡單的一個功能,還有很多等待著小伙伴去探索,我是感覺用了Idea就不想用Eclipse了,閑話說到這,下面看代碼,首先解釋一下,我用的是Idea社區辦2016.3
一、Struts簡單介紹
① 首先Struts是一個MVC框架,是Apache的一個開源框架,感覺哪里都有Apache這個組織
②?Struts 2以WebWork為核心,采用攔截器的機制來處理用戶的請求
二、Struts工作流程
① 客戶端(瀏覽器)發送請求
②?請求通過http協議發送給服務器
③ 服務器對請求進行攔截,這個是在web.xml文件中進行配置
④ web.xml文件對struts.xml文件映射
⑤ 映射到指定的action,返回resoult
⑥ 根據resoult指定對應的jsp頁面
?三、 Struts詳細配置
1 建立Java項目,這一步比較簡單,大家看一下就知道了
2 選擇Java,并勾選Struts2,默認選擇Doweload,這樣就不用添加Jar包了
?
3 添加項目名稱,點擊下一步即可,這時頁面會顯示在下載Jar包
?
4 整個項目的效果,這樣Jar包以及配置文件都已經好了
5 配置服務器,點擊右上角的,對服務器進行配置,我用的是Tomcat服務器,點擊+選擇Tomcat Server,選擇Local
6 對服務器進行配置,指定Tomcat路徑,端口等
7 點擊Deployment,點擊+,點擊Artifact,進行路徑配置,該路徑相當于Eclipse項目Web Root中的路徑,添加名字,點擊apply保存,服務器配置完成。
?
8 對于web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version="3.1"><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping> </web-app> View Code9 index.jsp頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html><head><title>登錄頁面</title></head><body><form action="${pageContext.request.contextPath}/test" method="post">username:<input name="username" type="text"><br>password:<input name="password" type="password"><br><input type="submit" value="提交"></form></body> </html> View Code10 struts.xml配置
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN""http://struts.apache.org/dtds/struts-2.5.dtd"><struts><package name="Struts" extends="struts-default"><action name="test" class="com.Demo"><result name="success">success.jsp</result><result name="error">error.jsp</result></action></package> </struts> View Code11 創建在com package下創建class Demo
package com;/*** Created by admin on 2018/3/30.*/ public class Demo {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;}public String execute(){if(username.equals("wyy")&password.equals("123456")){return "success";}return "error";} } View Code四、總結
當項目進行部署以后,會直接跳轉到index.jsp頁面,當頁面輸入用戶名,密碼以后,會發送http://localhost:8080/demo/test請求,首先需要注意該請求首先會被web.xml文件的url攔截,符合要求進行攔截,進行下一步,然后跳轉到com.Demo類中,對用戶名和密碼進行判斷,我現在用的是虛擬的數據,實際情況會訪問數據庫,看看數據庫中是否存在該用戶,并且密碼是否正確,真正的項目還會對密碼進行加密,符合要求后返回success,這時候struts.xml中的action對返回結果進行判斷,看看返回結果是否有success,然后進行頁面跳轉。
注:關于頁面數據傳遞,首先在form表單中配置字段的name,在com.Demo對字段提供get set方法,這樣就可以獲取頁面數據,注意字段的名字和類中屬性的名字必須保持一致。
轉載于:https://www.cnblogs.com/wyyDemoTest/p/8676782.html
總結
以上是生活随笔為你收集整理的IDEA配置Struts框架的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 古代六艺有哪些
- 下一篇: 四则运算题目生成程序