idea+tomcat+struts2 搭建一个web实例
參考這個博客搭建:
Struts2快速入門與使用IntelliJ IDEA創建第一個web項目
需要注意幾點:
1)可能會遇到struts-default高亮變紅的情況,其實是沒有把struts-default.xml加入到Default File Set
解決辦法:
2)注意struts的版本,2.3有ng,2.5沒有
3)注意要進行部署操作
4)加載TOMCAT的時候要記得配置Deployment
5)注意各個文件的路徑,庫文件的加載
在這個實例中,我把namespace="/hello",也就是當執行HelloAction的sayHi這個ACTION方法之后,如果返回success狀態,那么又因為在上面配置的namespace=hello,頁面跳轉時尋找規則為web/namespace/xxx.jsp,也即是:
web/hello/sayHi.jsp
因此我們在瀏覽器輸入http://localhost:8080/hello/sayHi 訪問項目
通過瀏覽器訪問http://localhost:8080/hello/sayHi,控制臺會打印HelloAction中的hello world,同時頁面會跳轉到struts.xml中配置的sayHi.jsp頁面,頁面顯示之前寫的SayHi文字。
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="hello" namespace="/hello" extends="struts-default"><action name="sayHi" class="com.southstar.demo.HelloAction" method="sayHi"><result name="success">sayHi.jsp</result></action></package> </struts>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_4_0.xsd"version="4.0"><display-name>Struts Blank</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> </web-app>Action
package com.southstar.demo;public class HelloAction {public String sayHi() {System.out.println("hello world");return "success";} }sayHi.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body> 沒魚,SB </body> </html>至此,一個完整的簡單struts實例,創建完成!
?
總結
以上是生活随笔為你收集整理的idea+tomcat+struts2 搭建一个web实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用Intellij Idea 创建一个
- 下一篇: struts2框架入门