springmvc如何配置能支持html?

主要异常信息是:Did not find handler method for [/index.html]
我在web.xml的欢迎页设置了两个:
index.html
index.jsp
我在项目根目录下有index.jsp;可以访问.当我把index改成index.html后就404错误;
用的是ssm框架
是不是springmvc不支持html视图?

2个回答

1.web.xml里如果配置拦截器为/ 那么需要加载静态资源

<servlet-mapping>  
    <servlet-name>default</servlet-name>  
    <url-pattern>*.png</url-pattern>  
    <url-pattern>*.jpg</url-pattern>  
    <url-pattern>*.gif</url-pattern>  
    <url-pattern>*.js</url-pattern>  
    <url-pattern>*.css</url-pattern>  
    <url-pattern>*.ico</url-pattern>  
    <url-pattern>*.zip</url-pattern>  
    <url-pattern>*.xml</url-pattern>  
    <url-pattern>*.mp3</url-pattern>  
  </servlet-mapping>  

*.html

加入

配置改成:*.html

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
Python+OpenCV计算机视觉

Python+OpenCV计算机视觉

springmvc返回html时转成jsp了报404,怎么同时支持解析jsp和html

配置如下: <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/view/"/> </bean> <bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean"></bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/> <property name="contentType" value="text/html; charset=utf-8"/> <property name="cache" value="true"/> <property name="suffix" value=".html"/> <property name="order" value="3"/> </bean> <bean id="viewResolverJsp" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"/> <property name="suffix" value=".jsp" /> <property name="order" value="2"/> </bean> controller的方法如下: @RequestMapping("helloHTML") public String helloHTML(HttpServletRequest request, HttpServletResponse response){ try{ System.out.println("Controller开始执行"); }catch(Exception e){ }finally{ return "helloTest"; } } 报错如下: HTTP Status 404 - /WEB-INF/view/helloTest.jsp type Status report message /WEB-INF/view/helloTest.jsp description The requested resource is not available.

springmvc中可以不用jsp吗

1.能不能直接让controller返回数据给html而不经过jsp这一层哪? 需要的数据直接让response.getWriter().write(json)写出去,用html页面中的js来接受处理数据,需要展示的页面通过controller的返回值return "target.html"来实现。 ,然后在springmvc的配置项里面也不去配置后缀为jsp了。 2.还有一个问题就是如果用了setAttribute(json)的话,是不是就必须用到jsp了?因为我觉得它是java对象如果没有jsp解析的话,html中的js是没法解析的。

SpringMVC 项目,JSP 视图解析器 直接jsp返回为html,配置错误?

Controller采用注解的方式,代码为: ```@Controller @RequestMapping("/request") public class TestController { @Autowired UIFieldService fieldService; @RequestMapping("/testController")//method=RequestMethod.GET,value= public String getString(){ //@RequestBody String str,HttpServletResponse response, Model model System.out.println("hello world"); return "test"; } } ``` dispatcherServlet.xml配置中jsp视图解析器配置: ``` <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name = "prefix" value="/WEB-INF/views/"></property> <property name = "suffix" value = ".jsp"></property> <property name="order" value="1"></property> </bean> ``` 感觉这个jsp视图解析器配置了没用,这个文件中还必须要配置<mvc:default-servlet-handler/>,否则提示错误。同时web.xml增加了下面一项: ``` <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jsp</url-pattern> </servlet-mapping> ``` 请求路径: http://devtest.milan.com:8082/test_c3p0/service/request/testController _返回结果: ![图片说明](https://img-ask.csdn.net/upload/201707/20/1500538613_302933.png)

SpringMVC html无法post到后台,提示404

html里面点击提交按钮,原本是想试一下ajax看看前后台能不能通,但是点击的时候,出现404。 初学者想请各位帮忙看看原因 ![图片说明](https://img-ask.csdn.net/upload/202004/14/1586828340_899772.png) html代码 ![图片说明](https://img-ask.csdn.net/upload/202004/14/1586828356_453339.png) controller代码,后面没有截全 @RequestMapping(method = RequestMethod.POST,produces = "application/json,charset=UTF-8",value = "/printHello") ![图片说明](https://img-ask.csdn.net/upload/202004/14/1586828388_713351.png) springdemo-servlet.xml ![图片说明](https://img-ask.csdn.net/upload/202004/14/1586828455_479273.png) ============================================================= 根据两位大佬的回答,把url改成localhost:8080/xxxx 或者是修改为/springdemo/printHello都可行。修改以后出现 ![图片说明](https://img-ask.csdn.net/upload/202004/14/1586844513_913949.png) 在调试中并没有进入到printHello方法,我想问一下这个是原因。。我根据网上的方法修改了tomcat的conf文件也没有生效 ============================================================= 问题已解决,根据前面修改了controller和url的基础上,再修改了一下ajax,就可以成功交互了 ![图片说明](https://img-ask.csdn.net/upload/202004/15/1586921196_37448.png)

请问如何让springmvc支持.jsp后缀访问后台方法

如题, 如果直接在web.xml加 ``` <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.jsp</url-pattern> </servlet-mapping> ``` 这样写访问浏览器直接提示无法重定向

SpringMVC页面访问404的问题

SpringMVC项目根路径路径下面的静态文件可以访问,但是访问@RequestMapping注解页面时就报404? web.xml ``` <?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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" id="WebApp_ID" version="2.5"> <display-name>test</display-name> <!-- - Location of the XML file that defines the root application context. - Applied by ContextLoaderListener. --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/application-config.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- - Servlet that dispatches request to registered handlers (Controller implementations). --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/mvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> ``` mvc-config.xml ``` <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.hourui.controller" /> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' --> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp" /> </bean> </beans> ``` Test.java ``` package com.hourui.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class Test { @RequestMapping("/hello") public String hello (){ return "showMessage"; } } ``` 当访问/hello页面时就会报404,但是静态html文件却可以直接访问,请指教?

post提交form表单到springMVC,不能跳转问题

**项目结构: ** ![图片说明](https://img-ask.csdn.net/upload/201812/14/1544768206_941826.png) 1. jsp页面 **index.jsp** ``` <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>首页</title> </head> <body> <a href="/sdkapp/init">去往登录页</a> </body> </html> ``` **login.jsp** ``` <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登录页</title> <script src="<%=request.getContextPath()%>/js/jquery-1.7.2.js" type="text/javascript"></script> </head> <body> <form id="frm"> 用户id: <input type="text" name="uid"><br><br> 用户名: <input type="text" name="username"><br><br> 密 码: <input type="password" name="password"><br><br> <input type="submit" id="btn" value="登录"> </form> <script type="text/javascript"> $("#btn").click(function () { console.log(getFormJson(frm)); $.ajax({ url: "${pageContext.request.contextPath}/sdkapp/login", type:"POST", data: getFormJson(frm), contentType:"application/json;charset=utf-8", success:function(data){ console.log(data) }, error:function(data){ console.log(data) } }); }) function getFormJson(frm) { //frm:form表单的id var o = {}; var a = $("#"+frm).serializeArray(); $.each(a, function() { if (o[this.name] !== undefined) { if (!o[this.name].push) { o[this.name] = [ o[this.name] ]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); return o; } </script> </body> </html> ``` **success.jsp** ``` <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登录成功页</title> </head> <body> <h1>登录成功</h1> </body> </html> ``` 2. 配置文件 **springmvc.xml** ``` <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--配置默认的servlet处理器,处理静态资源加载--> <mvc:default-servlet-handler/> <!--配置包扫描controller--> <context:component-scan base-package="controller"/> <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans> ``` **web.xml** <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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" id="WebApp_ID" version="2.5"> <servlet> <servlet-name>springMvc</servlet-name> <!--加载前端控制器--> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> 3.java代码 **Login.java** ``` package controller; import controller.vo.User; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @RestController @RequestMapping(value = "/sdkapp") public class Login { @RequestMapping(value = "/init") public ModelAndView casdkbindInput(){ ModelAndView view = new ModelAndView("/login"); return view; } @RequestMapping(value = "/login",method = {RequestMethod.POST},consumes = "application/json;charset=utf-8") public ModelAndView testJson(@RequestBody User user) throws Exception { System.out.println(user); ModelAndView view = new ModelAndView("/success"); return view; } } ``` **vo/User.java** ``` package controller.vo; import java.io.Serializable; public class User implements Serializable { private String uid; private String username; private String password; public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } 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; } @Override public String toString() { return "User{" + "uid='" + uid + '\'' + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; } } ``` 问题是 jq post请求到后台,后台返回 view时,前端接收不到? 附上demo地址:[百度云盘](https://pan.baidu.com/s/1edbTMGCw2wrhGKADY9x8XA "")

springMVC怎么可以访问.do和.jsp

结果:访问.do报404错误;![图片说明](https://img-ask.csdn.net/upload/201905/06/1557130784_736858.png) 访问.jsp可以正常访问: ![图片说明](https://img-ask.csdn.net/upload/201905/06/1557130799_855496.png) 项目结构: ![图片说明](https://img-ask.csdn.net/upload/201905/06/1557131293_133479.png) 两个Controller: ![图片说明](https://img-ask.csdn.net/upload/201905/06/1557130985_851585.png) 这个返回值是json格式的。 ![图片说明](https://img-ask.csdn.net/upload/201905/06/1557131017_288476.png) web.xml ``` <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>springmvc</display-name> <!-- Spring配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:bean.xml</param-value> </context-param> <!-- Bootstraps the root web application context before servlet initialization --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- DispatcherServlet配置 --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 为了能够处理中文的post请求,再配置一个encodingFilter,以避免post请求中文出现乱码情况 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> ``` springmvc.xml ``` <context:component-scan base-package="com.web"></context:component-scan> <!-- <aop:aspectj-autoproxy proxy-target-class="true" /> --> <!-- 静态资源(js、image等)的访问 --> <mvc:default-servlet-handler /> <!-- 开启注解 --> <mvc:annotation-driven /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans> ``` bean.xml ``` <!-- 1. 数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 省略啦!!!! </bean> <!-- 2. Spring 对SessionFactory创建支持 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="mappingLocations"> <list> <value>classpath:com.pojo/*.hbm.xml</value> </list> </property> </bean> <!-- 3. 事务管理 --> <!-- a. 配置事务管理器类(切面类) --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- b. 事务通知 --> <tx:advice id="txAdvice" transaction-manager="txManager"> </tx:advice> <!-- c. Aop配置 --> <aop:config> <aop:pointcut expression="execution(* *Service.*(..))" id="pt" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt" /> </aop:config> <!-- 公用的配置 abstract="true" 表示是一个抽象的bean,不会被实例化; 可以被其他bean继承,就可以省略父bean定义多的属性 --> <bean id="baseDao" abstract="true"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- dao配置 --> <bean id="userDao" parent="baseDao" class="com.dao.UserDao"></bean> <!-- service配置 --> <bean id="userService" class="com.service.UserService"> <property name="userDao" ref="userDao"></property> </bean> <!-- controller配置 --> <bean id="userController" class="com.controller.UserController"> <property name="userService" ref="userService"></property> </bean> </beans> ``` 请问怎么可以访问.do和.jsp呢?

springMVC上传文件报错404

学习springMVC文件上传的时候,选择了文件提交以后报错404 jsp ``` <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>文件上传</title> </head> <body> <h3>文件上传</h3> <form action="/User/fileUpload" method="post" enctype="multipart/form-data"> <input type="file" name="upload"><br/> <input type="submit" value="上传"> </form> </body> </html> ``` spring-MVC配置如下 ``` <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--开启注解扫描--> <context:component-scan base-package="com.ryn"/> <!--配置视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--配置解析器--> <property name="suffix" value=".jsp"/> <!--配置目录--> <property name="prefix" value="/WEB-INF/pages/"/> </bean> <!--配置静态资源--> <mvc:resources location="/upload/" mapping="/upload/**" /> <mvc:resources mapping="/js/**" location="js/"/> <!--配置文件解析器--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"/> <property name="maxUploadSize" value="10485760"/> </bean> <!--开启springMVC框架注解的支持--> <mvc:annotation-driven/> </beans> ``` web.xml配置 ``` <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!--配置前端控制器--> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> ``` uploadController ``` /** * 文件上传 * @return */ @RequestMapping("/fileUpload") public String fileUpload(HttpServletRequest request, @RequestParam("upload") MultipartFile upload) throws Exception{ System.out.println("文件上传"); //指定上传位置 String path = request.getSession().getServletContext().getRealPath("/upload/"); //判断文件夹是否存在 File file = new File(path); if (!file.exists()){ //若不存在,则创建文件夹 file.mkdirs(); } String filename = upload.getOriginalFilename(); //使用write方法上传文件 String uuid = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase(); filename = uuid + "_" + filename; upload.transferTo(new File(path,filename)); return "success"; } ``` 报错信息如下 ![图片说明](https://img-ask.csdn.net/upload/201911/14/1573725685_322217.jpg) 求各位大佬帮我看看问题到底出在哪了!!!弄了半个小时不知道怎么回事

springMVC+ehcache 配置没有问题,但是缓存不起作用

照着网上的例子用springMVC+ehcache 基于注解的方式 实现缓存功能,配置 没有报错,但是 却不能实现 缓存。 :( 后来 我又用原先成功实现缓存的springAOP拦截器的方式, 也还是不能实现缓存,我觉得 可能是有些冲突或者是被拦截了。 具体代码如下,大家看看吧(这里用了springMVC+mybatis+ehcache): spring-config.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd" default-lazy-init="true"> <context:annotation-config /> <context:component-scan base-package="com.myword"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- 载入properties文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:db.properties" /> </bean> <!-- 配置数据源(使用c3p0连接池 --> <!-- driverClass : 连接数据库的驱动; jdbcUrl : 连接数据库的URL ; user : 数据库用户名 ; password : 数据库用户密码 ; acquireIncrement : 当数据库缺乏连接时,需要增加多少新连接 ; idleConnectionTestPeriod : 连接在多久未使用之后自动关闭 ; maxPoolSize : 能够创建的最大连接数; maxStatements :一个链接能执行的最大sql语句数 ; minPoolSize : 创建的最小连接数 ; --> <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name="driverClassName" value="${db.driverClassName}"> </property> <property name="url" value="${db.url}"> </property> <property name="username" value="${db.username}"> </property> <property name="password" value="${db.password}"> </property> </bean> --> <!-- --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${db.driverClassName}" /> <property name="jdbcUrl" value="${db.url}" /> <property name="user" value="${db.username}" /> <property name="password" value="${db.password}" /> <property name="acquireIncrement" value="10" /> <property name="idleConnectionTestPeriod" value="60" /> <property name="maxPoolSize" value="100" /> <property name="maxStatements" value="50" /> <property name="minPoolSize" value="10" /> </bean> <!-- 配置sqlSessionFactory, 当使用MapperFactoryBean时configLocation没有必要添加 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation"> <value>classpath:mybatis.xml</value> </property> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations"> <list> <value>classpath:com/myword/mapper/*.xml</value> </list> </property> </bean> <!-- 扫描mapper接口--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name="basePackage" value="com.myword.mapper"></property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 开启使用注解配置事务 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> </beans> spring-mvc.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <context:component-scan base-package="com.myword" > <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan> <!-- 开启springmvc的注解支持 --> <mvc:annotation-driven /> <!-- 防止静态资源被过滤器拦截 --> <mvc:resources mapping="/extjs4/**" location="/extjs4/" /> <mvc:resources mapping="/app/**" location="/app/" /> <mvc:resources mapping="/res/**" location="/res/" /> <mvc:resources mapping="/console.js" location="/console.js" /> <mvc:resources mapping="/app.js" location="/app.js" /> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"/> <property name="maxUploadSize" value="800000000"/> </bean> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="ignoreAcceptHeader" value="true" /> <property name="defaultContentType" value="text/html" /> <property name="order" value="1" /> <!--扩展名至mimeType的映射,即 /user.json => application/json--> <property name="mediaTypes"> <map> <entry key="json" value="application/json" /> <entry key="xml" value="application/xml" /> </map> </property> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/" /> <property name="suffix" value=".jsp" /> <!--重定向返回默认添加后缀--> </bean> </list> </property> <property name="defaultViews"> <list> <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" /> </list> </property> </bean> <ehcache:annotation-driven /> <ehcache:config cache-manager="cacheManager"> <ehcache:evict-expired-elements interval="60" /> </ehcache:config> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" > <value>classpath:ehcache.xml</value> </property> </bean> 主要的配置 就是这两个,其他导入的 ehcache.xml和mybatis.xml就没有贴出来了。 service 的javabean: @Service public class UsersServI implements UsersServ{ @Autowired private UsersMapper usersMapper; @Cacheable("baseCache") public List<Users> getUsersAll(UsersExample example) { System.out.println("======ok======"); return usersMapper.selectByExample(example); } action 中的打印处理: Cache baseCache = manager.getCache("baseCache"); System.out.println("Cache named baseCache:"+baseCache); System.out.println("============one==============="); UsersExample example = new UsersExample(); usersServ.getUsersAll(example); System.out.println("============two==============="); usersServ.getUsersAll(example); 结果发现 执行后 后台打印出: Cache named baseCache:[ name = baseCache status = STATUS_ALIVE eternal = false overflowToDisk = true maxEntriesLocalHeap = 10000 maxEntriesLocalDisk = 0 memoryStoreEvictionPolicy = LRU timeToLiveSeconds = 600000 timeToIdleSeconds = 300000 persistence = none diskExpiryThreadIntervalSeconds = 120 cacheEventListeners: net.sf.ehcache.statistics.LiveCacheStatisticsWrapper hitCount = 0 memoryStoreHitCount = 0 diskStoreHitCount = 0 missCountNotFound = 0 missCountExpired = 0 maxBytesLocalHeap = 0 overflowToOffHeap = false maxBytesLocalOffHeap = 0 maxBytesLocalDisk = 0 pinned = false ] ============one=============== ======ok====== ============two=============== ======ok====== 第一个打印"Cache named ..."说明ehcache配置是成功的,ehcache.xml中的cache name 也被打印出来了。 但是 后面getUsersAll方法却被执行了两次 说明 ehcache根本没有起作用。到底哪里出问题了呀?

spring+springMVC+mybatis+redis的整合(注解方式)可以返回数据,但是不能缓存到redis中的问题?

``` <!-- 读取数据库的配置 --> <context:property-placeholder location="classpath:config.properties,classpath:config/redis.properties" /> <cache:annotation-driven cache-manager="redisCacheManager"/> <!-- 创建一个jedis并设置 IP、port、password--> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <!-- <property name="poolConfig" ref="poolConfig"/> --> <property name="hostName" value="${redis.url}"/> <property name="port" value="${redis.port}"/> <property name="password" value="${redis.auth}"/> <property name="database" value="${redis.dbIndex}"/> <!-- <property name="timeout" value="20000" ></property> --> </bean> <!-- redis 序列化策略 ,通常情况下key值采用String序列化策略, --> <!-- 如果不指定序列化策略,StringRedisTemplate的key和value都将采用String序列化策略; --> <!-- 但是RedisTemplate的key和value都将采用JDK序列化 这样就会出现采用不同template保存的数据不能用同一个template删除的问题 --> <bean id="stringSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/> <bean id="jdkSerializerRedis" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" > <property name="connectionFactory" ref="jedisConnectionFactory" /> <property name="keySerializer" ref="stringSerializer"/> <!-- key使用String序列化方式 --> <property name="valueSerializer" ref="stringSerializer"/><!-- value使用jdk序列化方式 --> </bean > <!-- 配置RedisCacheManager --> <bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"> <constructor-arg name="redisOperations" ref="redisTemplate"/> <property name="defaultExpiration" value="${redis.expiration}"/> </bean> <aop:aspectj-autoproxy proxy-target-class="true"/> ``` 我还序列化了实体类,在控制层@Cacheable(value ="user")希望把返回的数据,保存到redis中去。 现在能获取到数据,但是不能缓存到redis中去。后台不报错。有哪位大佬遇见过这种情况。给小老弟讲解讲解呗

springMVC 这里怎样才能访问到静态资源

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"> <!-- mavaen创建先修改webapp为3.1 --> <!-- 配置DispatherServlet --> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--配置springMVC需要加载的配置文件 spring-dao.xml,spring-service.xml,spring-web.xml Mybatis->spring->springMVC --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-*.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <!--默认匹配所有请求 --> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--配置springmvc--> <!--1:开始SpringMVC注解模式--> <!--简化配置:--> <!--1)自动注册DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter--> <!--2)提供一些列:数据绑定,数字和日期的format @NumberFormat @DataTimeFormart,xml,json 默认读写支持。--> <mvc:annotation-driven/> <!--2.静态资源默认servlet配置--> <!-- 1).加入对静态资源处理:js,gif,png 2).允许使用 "/" 做整体映射 --> <mvc:default-servlet-handler/> <!--3:配置JSP 显示ViewResolver--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!--4:扫描web相关的bean--> <context:component-scan base-package="com.triggerpoint.*"/> </beans> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html class="no-js"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1,user-scalable=no"> <meta name="renderer" content="webkit"> <meta http-equiv="Cache-Control" content="no-siteapp"/> <!-- 启用360浏览器的极速模式(webkit) --> <meta name="renderer" content="webkit"> <!-- 避免IE使用兼容模式 --> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <!-- 针对手持设备优化,主要是针对一些老的不识别viewport的浏览器,比如黑莓 --> <meta name="HandheldFriendly" content="true"> <!-- 微软的老式浏览器 --> <meta name="MobileOptimized" content="320"> <!-- uc强制竖屏 --> <meta name="screen-orientation" content="portrait"> <!-- QQ强制竖屏 --> <meta name="x5-orientation" content="portrait"> <!-- UC强制全屏 --> <meta name="full-screen" content="yes"> <!-- QQ强制全屏 --> <meta name="x5-fullscreen" content="true"> <!-- UC应用模式 --> <meta name="browsermode" content="application"> <!-- QQ应用模式 --> <meta name="x5-page-mode" content="app"> <!-- windows phone 点击无高光 --> <meta name="msapplication-tap-highlight" content="no"> <!-- 适应移动端end --> <title>水果拼团网站</title> <link rel="stylesheet" href="/resources/css/frameui.css"/> <link rel="stylesheet" href="/resources/css/widget/slider.css"/> <link rel="stylesheet" href="/resources/css/index.css"/> </head> <body> <!--a1轮播图--> <div data-am-widget="slider" class="am-slider am-slider-a1" data-am-slider='{"directionNav":false}' > <ul class="am-slides cl"> <li> <img src="images/j-d01.jpg"> </li> <li> <img src="images/j-d02.jpg"> </li> </ul> </div> <!--介绍--> <div class="details-body-box"> <div class="tm"> <div class="td2"> <div class="td2_price"><span>已售:<b>948</b>件</span></div> <div class="cl"></div> <div class="td2_name"> 越南进口白肉火龙果 </div> <div class="td2_cx">外皮红艳,果肉绵软白嫩,汁液丰富,富含维生素C.</div> </div> </div> <div class="tm2"> <div class="td2_info"> <div class="td2_num">支付开团并邀请<span id="tuan_more_need_number">2</span>人参团,人数不足自动退款</div> </div> <div style="visibility: visible;" class="kt"> <div class="pin"> <div class="pin_btn_box"> <div class="pin_btn"> 3人团 </div> </div> <div class="pin_btn_box"> <div class="pin_btn pin_btn_bg2"> 原价 </div> </div> <div class="cl"></div> </div> <div> <a href="javascript:void(0);" class="kt_item kt_item_buy "> <div class="kt_price"><b>¥16.80</b>&nbsp;/&nbsp;700g~800g</div> <div class="kt_btn" id="tuan_one_number">原价购买</div> </a> <a href="pay.html" class="kt_item"> <div class="kt_price"><b>¥14.00</b>&nbsp;/&nbsp;700g~800g</div> <div class="kt_btn">立即拼团<br></div> </a> </div> </div> </div> <!--拼团玩法--> <div class="step"> <div class="step_hd"> 拼团玩法<a class="step_more" href="tuan_rule.html">查看详情</a> </div> <div id="footItem" class="step_list"> <div class="step_item step_item_on"> <div class="step_num">1</div> <div class="step_detail"> <p class="step_tit">选择 <br>心仪商品</p> </div> </div> <div class="step_item "> <div class="step_num">2</div> <div class="step_detail"> <p class="step_tit">支付开团 <br>或参团</p> </div> </div> <div class="step_item "> <div class="step_num">3</div> <div class="step_detail"> <p class="step_tit">等待好友 <br>参团支付</p> </div> </div> <div class="step_item"> <div class="step_num">4</div> <div class="step_detail"> <p class="step_tit">达到人数 <br>团购成功</p> </div> </div> </div> </div> <div class="outs-box"> <h1>商品详情</h1> <img src="/resources/images/d1.jpg"> <img src="/resources/images/d2.jpg"> <img src="/resources/images/d3.jpg"> </div> </div> <!--detail_footer begin--> <footer class="detail_footer"> <div class="return_left_box"> <a href="index.html"><div class="return_home fl"></div></a> <a href="javascript:void(0);"><div class="return_home share_tuan fl"></div></a> </div> <a href="pay.html"><div class="promptly_tuan fl">立即试用</div></a> <div class="cl"></div> </footer> <!--detail_footer end--> <!--引入js资源--> <script src="/resources/js/jquery.min.js"></script> <script src="/resources/js/amazeui.js"></script> <script src="/resources/js/handlebars.min.js"></script> <script src="/resources/js/amazeui.widgets.helper.js"></script> </body> </html>

springmvc websocket +tomcat,同时支持的连接数太小了 怎么办?

问题:我想通过tomcat发布一个websocket程序,代码都已经调通。但是,前端并发最多只能支持260websocket连接就上不去了,以下是我的前端测试代码和tomcat的配置。请大家帮我看看是我写错了么,还是说单个tomcat的瓶颈就是如此? 前端代码 ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> <script src=" <%=request.getContextPath() %>/js/jquery/jquery-1.11.3.js"></script> <script src=" <%=request.getContextPath() %>/js/others/sockjs-0.3.min.js"></script> <script src=" <%=request.getContextPath() %>/js/others/stomp-2.3.3.js"></script> </head> <body> <input type="button" value="连接" onclick="opens()" /> <input type="button" value="断开" onclick="closes()" /> <select id="type"> <option value=1>点对点</option> <option value=2>订阅</option> <option value=3>主题订阅</option> </select> <input type="button" value="发送" onclick="send()"/> 用户名:<input type="text" id="name" value="bulbuls"/> <hr/> <table> <tr> <td>消息</td> <td><input type="text" id="msg"/></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td><td></td> </tr> <tr> <td>message</td> <td><div id="message"></div></td> <td>返回的消息</td> <td><div id="recode"></div></td> </tr> </table> <hr/> <script> var i = 0; function opens(){ i++; if(i>300){ return; } var socket = new SockJS("http://127.0.0.1:8080/hello"); var stompClient = Stomp.over(socket); stompClient.connect({}, function(frame) { message("连接成功....:"+i); //全域广播 stompClient.subscribe('/topic', function(greeting){ $("#recode").append("<div>"+greeting.body+"</div>"); }); opens(); //主题广播 /* stompClient.subscribe('/topic/'+$("#name").val(), function(greeting){ $("#recode").append("<div>"+greeting.body+"</div>"); }); */ //点对点方式回调 /* stompClient.subscribe('/user/'+$("#name").val()+'/point', function(message){ $("#recode").append("<div>"+message.body+"</div>"); }); */ }); } function closes(){ if (stompClient != null) { stompClient.disconnect(); message("断开连接....") } } function send(){ if(!stompClient || !stompClient.connected){ message("警告:连接已经断开"); return; } var msg = $("#msg").val(); var name = $("#name").val(); var type = $("#type").val(); if(type==1){ stompClient.send("/app/point", {}, JSON.stringify({"user":"bulbuls","destination":"/point","message":"来自手机的消息"})); }else if(type==2){ stompClient.send("/app/topic", {}, JSON.stringify({"user":"bulbuls","destination":"/topic","message":"来自手机的消息"})); }else if(type==3){ stompClient.send("/app/topic/bulbuls", {}, JSON.stringify({"user":"bulbuls","destination":"/point","message":"来自手机的消息"})); } } function message(mess){ $("#message").append("<div>"+mess+"</div>"); } </script> </body> </html> ``` tomcat server.xml配置 ``` <Service name="Catalina"> <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="400" minSpareThreads="4"/> <Connector connectionTimeout="20000" port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443" maxThreads="300"/> <!-- Define an AJP 1.3 Connector on port 8009 --> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/> <Engine defaultHost="localhost" name="Catalina"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <!-- This Realm uses the UserDatabase configured in the global JNDI resources under the key "UserDatabase". Any edits that are performed against this UserDatabase are immediately available for use by the Realm. --> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log." suffix=".txt"/> <Context docBase="C:\soft\apache-tomcat-7.0.73\wtpwebapps\websocket" path="/" reloadable="true" source="org.eclipse.jst.j2ee.server:websocket"/></Host> </Engine> </Service> ``` ![图片说明](https://img-ask.csdn.net/upload/201611/16/1479293125_809781.png) ![图片说明](https://img-ask.csdn.net/upload/201611/16/1479293133_835956.png)

springMVC项目跑起来却访问不了静态资源文件?

springmvc访问不了静态资源文件 ``` <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>springmvc</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-name>springmvc</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>loginIndex.jsp</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <description>字符集过滤器</description> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <description>字符集编码</description> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <!-- 路径映射 --> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>*.html</url-pattern> </filter-mapping> </web-app> ``` web.xml ``` <!-- 1.自动扫描包,实现支持注解的IOC --> <!-- 自动扫描该包,使springmvc认为包下用了@Controller注解的类是控制器 --> <context:component-scan base-package="com.bi.controller" /> <!-- 2.配置注解的处理器映射器和处理器适配器 --> <mvc:annotation-driven /> <!-- 3.Spring MVC不处理静态资源 --> <mvc:default-servlet-handler/> <mvc:resources mapping="/assets/**" location="/WEB-INF/assets/" cache-period="31536000"/> <mvc:resources mapping="/images/**" location="/WEB-INF/images/" cache-period="31536000"/> <!-- 4.配置内部视图解析器 --> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> ``` spring.xml ![访问不了文件](https://img-ask.csdn.net/upload/201903/20/1553091701_202958.png) ![报错信息](https://img-ask.csdn.net/upload/201903/20/1553091816_384277.jpg) 尝试过,在servlet加默认的方式,不行。 是不是哪里还有问题,或者是页面写法有问题? ``` <link rel="stylesheet" href="assets/css/bootstrap.min.css"> <link rel="stylesheet" href="assets/css/font-awesome.min.css"> <link rel="stylesheet" href="assets/css/themify-icons.css"> ```

SpringMVC中怎么将@ResponseBody返回Json数据在log4j中输出

spring配置如下: <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 --> </list> </property> </bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> controller如下: @RequestMapping("/saveUser") @ResponseBody public User saveUser(){ User user = new User(); user.setId("11"); user.setName("admin"); user.setPwd("11111111111111111111111"); user.setCreate_datetime(TimeUtils.getTransTime()); //int i = userService.addUser(user); return user; }

SpringMVC的@Requestbody接收不到json的值

控制层 @Controller @RequestMapping("/task") public class TaskAction { @RequestMapping("/run") public @ResponseBody TaskConnects runTask(@RequestBody TaskConnects connects)throws Exception{ System.out.println("----------------------" + "\n" + connects); return connects; } } js的代码 $.ajax({ type: "post", url: "task/run.action", contentType:'application/json;charset=utf-8', data: '{"ConnectionId":"con_12","SourceId":"circle-1","TargetId":"roundedRect-3"}', datatype: "json", success: function () { alert("send successfully") } SpringMVC配置文件也加了json转换器 <!-- json转换器 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJackson2HttpMessageConverter" /> </list> </property> </bean> <bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>text/json;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> <mvc:annotation-driven />

整了一晚上SpringMVC,就是出不来!

## 为了方便大神们的在线阅读,我把截图贴出来,再为了方便大神们帮我测试,我把代码也贴在截图下面,谢谢! ## 好了,开始贴截图+代码: ## 1,控制器 ![图片说明](https://img-ask.csdn.net/upload/201606/28/1467126416_712533.jpg) @Controller @RequestMapping("/Hello") public class HelloWorldController { @RequestMapping("/a") public ModelAndView test1(){ System.out.println("测试一下"); ModelAndView andView = new ModelAndView(); andView.addObject("msg", "供视图引用的一段信息"); andView.setViewName("/WEB-INF/views/Hello.jsp"); return andView; ## 2,配置文件 ![图片说明](https://img-ask.csdn.net/upload/201606/28/1467126648_273951.jpg) <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.cailikun" /> <!-- 开启对spring注解的支持 ,为了低版本兼容(Spring3.2及以上已自带) --> <context:annotation-config /> <!-- 静态资源访问 --> <mvc:default-servlet-handler /> <!-- SpringMVC开启注解支持,也是为了低版本兼容 --> <mvc:annotation-driven /> <!-- 配置上传解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设置上传文件的最大尺寸为1MB --> <property name="maxUploadSize"> <value>1048576</value> </property> </bean> </beans> ## 3,web.xml,有点长就不截图了,直接拷代码,抱歉 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SpringMVC</display-name> <!-- 放置到最前面:处理中文乱码的过滤器 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!-- 告诉spring 按照什么编码进行转码 --> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- 告诉spring配置文件的放置从哪里加载 --> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <!-- 配置了这个后,当服务器启动时,就开始实例化SpringMVC的核心控制器DispatcherServlet(有且只有一次) --> <!-- 也就是说:服务器启动变慢,访问变快 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- url-pattern常用的两种配置方式: --> <!--1, *.do 拦截以do为后缀的请求 --> <!--2, / --> <url-pattern>/</url-pattern> </servlet-mapping> <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,jsp视图 ![图片说明](https://img-ask.csdn.net/upload/201606/28/1467126821_910827.jpg) ## 求拯救!实在不知道怎么办了,检查了下被加载到tomcat里的文件,发现了一些疑问,怀疑也可能是tomcat的问题,这个链接就是后续提问:http://ask.csdn.net/questions/266317

SpringMVC使用注解解析json总是报415错误

用HTML访问这个总是报415错, 这是Spring 配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--指明 controller 所在包,并扫描其中的注解--> <context:component-scan base-package="com.ch.controller"/> <!-- 静态资源(js、image等)的访问 --> <mvc:default-servlet-handler/> <!-- 开启注解 --> <mvc:annotation-driven /> <!--ViewResolver 视图解析器--> <!--用于支持Servlet、JSP视图解析--> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 表示JPA Repository所在的包 --> <!-- 链接到persistence.xml --> <!-- 事务管理 --> <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/bookshop"></property> <property name="username" value="root"/> <property name="password" value="689571"/> <!-- 数据库连接池保持的最小连接数 --> <property name="minIdle" value="5"/> <!-- 数据库连接池保持的最大连接数 --> <property name="maxIdle" value="30"/> <!-- 当数据库连接因为某种原因断掉之后,再重新从连接池中拿另外一个连接时实际上这个连接可能 已经无效,所以为了确保所拿到的连接全都有效需要在获取连接,返回连接以及连接空闲时进行 有效性验证 下面3个设置为ture时进行验证,默认为false --> <!-- 取得连接时是否进行有效性验证 --> <property name="testOnBorrow" value="true"/> <!-- 返回连接时是否进行有效性验证 --> <property name="testOnReturn" value="true"/> <!-- 连接空闲时是否进行有效性验证 --> <property name="testWhileIdle" value="true"/> </bean> <!-- 设置Hibernate sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="datasource"/> <property name="packagesToScan" value="com.ch.model"/> <property name="hibernateProperties"> <props> <prop key="dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.transaction.coordinator_class">jdbc</prop> </props> </property> </bean> <!-- 设置Spring事务 --> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!--启动spring事务注解功能--> <tx:annotation-driven transaction-manager="txManager"/> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter"/> </list> </property> </bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> </list> </property> </bean> </beans> WEB配置文件 <?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"> <display-name>SpringMVCDemo Web Application</display-name> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> 所有的包和依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ch123</groupId> <artifactId>myspringtest</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>bookahop Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version>4.2.0.RELEASE</spring.version> <spring-data.version>1.2.0.RELEASE</spring-data.version> </properties> <dependencies> <!-- hibernate 4.0 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>${spring-data.version}</version> </dependency> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.0.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.2.2.Final</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </dependency> </dependencies> <build> <finalName>bookshop</finalName> </build> </project> 控制器 package com.ch.controller; import com.ch.bean.OBeanBase; import com.ch.bean.userinfo.userloginIbean; import com.ch.model.UsersEntity; import com.ch.service.UserService; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; /** * Created by 57183 on 2016/3/29. */ @Controller public class MainController { @RequestMapping(value = "/my", method = RequestMethod.POST) public String index(@RequestParam String userID,String password) { UsersEntity use=new UsersEntity(userID,password); UserService us = new UserService(); if (us.checklogin(use)) { return "index"; } else { return "err"; } } HTML代码: <html> <head> <title>Spring Hello World!</title> </head> <body> <form action="http://localhost:8080/my" method="POST" enctype="application/x-www-form-urlencoded"> 用户名<input type="text" name="userID"> 密码<input type="password" name="password"> <input type="submit" value="提交"> </body> </html> 求解为什么一直是415错误,网上的方法都试过了依旧没用。

springmvc+ajax上传图片的问题。传过去的是空值,这是为什么?

html代码 <form id="uploadForm" enctype="multipart/form-data"> <div class="email"> <label for="email-input">邮箱*</label> <input id="email" name="email" type="email"/> </div> <div class="name"> <label for="name-input">账号*</label> <input id="account" name="account" type="text"/> </div> <div class="website"> <label for="website-input">密码*</label> <input id="password" name="password" type="password"/> </div> <div class="name"> <label for="name-input">昵称*</label> <input id="nickname" name="nickname" type="text"/> </div> <div class="message"> <label for="message-input">介绍*</label> <textarea id="introduce" name="introduce" ></textarea> </div> <div class="website"> <label >头像*</label> <input id="file" type="file"/> </div> <div id="picTip"></div> <div class="submit"> <input type="button" value="注册" id="btn" name="btn"/> </div> </form> ----------------------------------------------------------------------------------- js代码: <script type="text/javascript"> $(function () { $("#btn").click(function () { var f = $("#file").val(); if(f==null||f==""){ $("#picTip").html("<span style='color:Red'>错误提示:上传文件不能为空,请重新选择文件</span>"); }else{ var extname = f.substring(f.lastIndexOf(".")+1,f.length); extname = extname.toLowerCase();//处理了大小写 if(extname!= "jpeg"&&extname!= "jpg"&&extname!= "gif"&&extname!= "png"){ $("#picTip").html("<span style='color:Red'>错误提示:格式不正确,支持的图片格式为:JPEG、GIF、PNG!</span>"); } } adduser(); }); }); function adduser(){ var email=$("#email").val(); var account=$("#account").val(); var password=$("#password").val(); var nickname=$("#nickname").val(); var introduce=$("#introduce").val(); var formData = new FormData($("#uploadForm")[0]); alert(formData) alert(email) $.ajax({ url:"./zhuce", data:{"email":email,"account":account,"password":password,"nickname":nickname,"introduce":introduce,"formData":formData}, type:"post", async: false, cache: false, contentType: false, processData: false, success: function (data) { if(data=="error"){ alert("注册失败!") } if(data=="success"){ alert("成功!") window.location.href="login.jsp" } } }); } </script> ----------------------------------------------------------------------- 后台controller代码: @RequestMapping(value="/zhuce") @ResponseBody public String zhuce(String email,String account,String password,String nickname,String introduce,@RequestParam(value="file",required = false) CommonsMultipartFile file,HttpServletRequest request, Model model){ System.out.println("邮箱和账号"+email+","+account); System.out.println("hhhh"); System.out.println("filename:"+file.getOriginalFilename()); String path = request.getSession().getServletContext().getContextPath(); System.out.println("path:"+path); return "success"; } ---------------------------------------------------------------- 运行结果是: 邮箱和账号null,null hhhh java.lang.NullPointerException--获取文件名字这里是空的

2019 Python开发者日-培训

2019 Python开发者日-培训

150讲轻松搞定Python网络爬虫

150讲轻松搞定Python网络爬虫

设计模式(JAVA语言实现)--20种设计模式附带源码

设计模式(JAVA语言实现)--20种设计模式附带源码

YOLOv3目标检测实战:训练自己的数据集

YOLOv3目标检测实战:训练自己的数据集

java后台+微信小程序 实现完整的点餐系统

java后台+微信小程序 实现完整的点餐系统

三个项目玩转深度学习(附1G源码)

三个项目玩转深度学习(附1G源码)

初级玩转Linux+Ubuntu(嵌入式开发基础课程)

初级玩转Linux+Ubuntu(嵌入式开发基础课程)

2019 AI开发者大会

2019 AI开发者大会

玩转Linux:常用命令实例指南

玩转Linux:常用命令实例指南

一学即懂的计算机视觉(第一季)

一学即懂的计算机视觉(第一季)

4小时玩转微信小程序——基础入门与微信支付实战

4小时玩转微信小程序——基础入门与微信支付实战

Git 实用技巧

Git 实用技巧

Python数据清洗实战入门

Python数据清洗实战入门

使用TensorFlow+keras快速构建图像分类模型

使用TensorFlow+keras快速构建图像分类模型

实用主义学Python(小白也容易上手的Python实用案例)

实用主义学Python(小白也容易上手的Python实用案例)

程序员的算法通关课:知己知彼(第一季)

程序员的算法通关课:知己知彼(第一季)

MySQL数据库从入门到实战应用

MySQL数据库从入门到实战应用

机器学习初学者必会的案例精讲

机器学习初学者必会的案例精讲

手把手实现Java图书管理系统(附源码)

手把手实现Java图书管理系统(附源码)

极简JAVA学习营第四期(报名以后加助教微信:eduxy-1)

极简JAVA学习营第四期(报名以后加助教微信:eduxy-1)

.net core快速开发框架

.net core快速开发框架

玩转Python-Python3基础入门

玩转Python-Python3基础入门

Python数据挖掘简易入门

Python数据挖掘简易入门

微信公众平台开发入门

微信公众平台开发入门

程序员的兼职技能课

程序员的兼职技能课

Windows版YOLOv4目标检测实战:训练自己的数据集

Windows版YOLOv4目标检测实战:训练自己的数据集

HoloLens2开发入门教程

HoloLens2开发入门教程

微信小程序开发实战

微信小程序开发实战

Java8零基础入门视频教程

Java8零基础入门视频教程

相关热词 c# 按行txt c#怎么扫条形码 c#打包html c# 实现刷新数据 c# 两个自定义控件重叠 c#浮点类型计算 c#.net 中文乱码 c# 时间排序 c# 必备书籍 c#异步网络通信
立即提问