首页 > 应用开发 > Java > Myeclipse怎样创建并运行web项目
https://blog.csdn.net/active2489595970/article/details/70393684
如何建立一个web jsp项目;当运行的时候回自动启动端口9090的服务
1.通过页面来显示内容
http://localhost:9090/Pro_jsp/index.jsp
或者运行指定的.jsp
http://localhost:9090/Pro_jsp/comm/bigdata/upload2hdfs.jsp
2.参照文档引入 各种jar包;引入各种layui的.css文件
3.在src中建立文件夹
the basepath is: http://localhost:9090/Pro_jsp/
http://localhost:9090/GISDSS_BD2/upload2hdfs.do
/com/bigdata/log/controller /com/bigdata/log/dao /com/bigdata/log/service
在里面建立hbase相关的类,定义不同功能的函数
4.我想在.js中使用 .do,但是如何配置呢?
在webBoot/WEB-INF/web.xml中添加下面的就打不开正常的index.jsp了
--4.1正常显示index.jsp页面
<?xml version="1.0" encoding="UTF-8"?>
<!-- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> -->
<web-app xmlns:web="http://java.sun.com/xml/ns/javaee" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>Pro_jsp</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
--4.2提示错误显示页面
<?xml version="1.0" encoding="UTF-8"?>
<!-- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> -->
<web-app xmlns:web="http://java.sun.com/xml/ns/javaee" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>Pro_jsp</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 增加下面的配置原本希望的是能够关联到controller的.do文件,但是 http://localhost:9090/Pro_jsp/index.jsp 提示
-->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
--5.(20200814) SpringMVC中jsp怎么调用controller中的某个方法
Spring MVC中jsp要调用baicontroller的某个方法需要用ajax或者dudwr来实现。
ajax举例:
var param="userIdSet="+"";
$.ajax({
url: "url 什么zhi点do,或者action",
// 数据发送方式
type: "post",
// 接受数据格式
dataType : "json",
// 要传dao递的数据
data :param,
// 回调函数,接受服务器端返回给客户端的值,即result值
success : function a(result){
回调
});
其中url参数就是controller在web.xml映射的.do或者.action。
里面对应的方法就调用到了。
--------------------begin
首先定义servlet
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
然后再在<servlet-mapping>中使用它
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.view</url-pattern>
</servlet-mapping>
--------------------end
--20200817经过肖剑的指导,需要增加 applicationContext.xml文件内容如下这个功能用来连接.do
<!-- 支持注解 -->
<context:component-scan base-package="com.*"></context:component-scan>
--20200817 今天写了一个方法,把图片写到了指定的文件夹
--step1
///////////////////begin 定义工具类,来获取当前Tomcat编译以后的路径,这个和java工程所在的路径不同,一个是java源文件路径,一个是
//javac编译以后的字节码路径,java图片是存放在字节码文件里面的
package com.idt.tools;
import java.io.File;
import java.net.URLDecoder;
public class RootPath {
static public String getWebRootPath(){
String path = Thread.currentThread().getContextClassLoader().getResource("").getFile();
File file = new File(path);
path = file.getParentFile().getParentFile().getAbsolutePath();
try {
path = URLDecoder.decode(path, "UTF-8");
path = path.replaceAll("\\\\", "/") ;
} catch (Exception e) {
}
return path ;
}
}
///////////////////////////////////end
--step2
//引入工具类
import com.idt.tools.*;
//各种代码begin
//保存文件到指定文件夹
saveImageAsOtherDir(file,name);
//各种代码end
--step3
/***
* 20200817 向指定的编译环境path文件夹保存图片文件
* @param file
* @param filename
* @throws Exception
*/
public void saveImageAsOtherDir(MultipartFile file, String filename) throws Exception {
File f = null;
if(file.equals("")||file.getSize()<=0){
file = null;
}else{
InputStream ins = file.getInputStream();
//使用工具类方法
String path = new RootPath().getWebRootPath();
//下面的是linux的写法
//f=new File("\\Pro_jsp\\src\\com\\bigdata\\log\\img\\1.png");
//下面的是windows中获取的路径,需要指定d:\是哪个盘符
//f=new File("d:\\1.png"); copy成功
//获取文件的扩展名
int i = filename.lastIndexOf(".");
String extName = filename.substring(i);
//为了防止文件名重复,对文件名生成一个uuid
String uuid = UUID.randomUUID().toString().replace("-", "");
//文件编译以后的path,放在webRoot下面 D:\Tomcat8.0-dashuju\webapps\Pro_jsp\comm\img\ahphj9871023iuoiausdf0871234.png
String tgt_file = path + "\\comm\\img\\" + uuid + extName;
//构造目标文件
f=new File(tgt_file);
try {
//构造目标文件流对象
FileOutputStream os = new FileOutputStream(f,true);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/450962/viewspace-2711421/ ,如需转载,请注明出处,否则将追究法律责任。