Intellij IDEA 配置最简单的maven-struts2环境的web项目
在idea里搭建maven项目
看着网上大神发的各种博客,然后自己搭建出来一个最简单的maven-strtus2项目,供初学者学习
- 新建project
下一步 什么都不选,直接finish - idea里的jar包管理都是通过pom.xml来实现的,下面就开始配置pom.xml文件,来给项目加上struts2的jar包
- 很多人会对pom.xml里的依赖配置迷惑,给大家推荐一个网站,里面有各种jar的依赖配置 http://maven.oschina.net/index.html#nexus-search;quick~struts
- 将依赖拷贝到pom.xml里保存,idea会自动下载jar文件到你本地安装的maven配置的库里(这里把jstl的包也添加一下)
- 开始配置项目的服务器,添加web支持等
将服务器添加上后,在添加web支持
- 接下来配置web.xml,struts.xml,并添加action包,以及action类,我这就直接贴代码
web.xml
1234567891011121314151617<?xml version="1.0"encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"version="2.5"><filter><filter-name>struts</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>struts.xml
12345678910111213141516171819202122<?xml version="1.0"encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"<struts><!-- 支持动态调用 --><constant name="struts.enable.DynamicMethodInvocation"value="true"/><!-- 设置开发模式 --><constant name="struts.devMode"value="true"/><packagename="front"namespace="/"extends="struts-default"><action name="user"class="hello.HelloAction"><result name="success">/index.jsp</result></action></package></struts>HelloAction.java
1234567891011121314151617181920packagehello;importcom.opensymphony.xwork2.ActionSupport;importorg.apache.struts2.ServletActionContext;importjavax.servlet.http.HttpServletRequest;/*** Created by Yang on 14-3-27.*/publicclassHelloActionextendsActionSupport{HttpServletRequest request = ServletActionContext.getRequest();publicString hello() {request.setAttribute("hello","hello world!");returnSUCCESS;}}index.jsp
下面是项目的位置:1234567891011121314151617<%--Created by IntelliJ IDEA.User: YangDate:14-3-27Time: 下午5:16To changethistemplate use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8"language="java"%><html><head><title></title></head><body>thisis a jsp page.<br> ${hello}</body></html> - 下面配置tomcat服务器
没有配置tomcat的需要先配置一个tomcat,配置方法就不发了,直接发布项目吧
ok,这样项目就配置到对应的服务器了,现在只需要启动服务器等待就可以了
- 在浏览器里输入http://localhost:8080/maven-struts/hello!hello
访问的结果如图:大功告成

浙公网安备 33010602011771号