I hava a project with springmvc
my services folder
my spring servlet-context.xml
<annotation-driven />
<context:component-scan base-package="com.ldl.origami.origamiService" />
Other services are working well
#Service
public class ServerEndPointService {
#Autowired(required = false)
private NewsMapper newsMapper;
public int getUnReadNews(String userName) {
NewsExample example = new NewsExample();
example.createCriteria().andUsernameEqualTo(userName).andStateEqualTo(0);
List<News> news = newsMapper.selectByExample(example);
return news.size();
}
}
this is a websocket ServerEndpoint
#ServerEndpoint(value = "/serverEndPoint/{userName}")
public class SystemServerEndPoint
I want to inject AdminService to SystemServerEndPoint,so I tried
#ServerEndpoint(value = "/serverEndPoint/{userName}")
public class SystemServerEndPoint{
#Inject
private ServerEndPointService serverEndPointService;
and
#Component
#ServerEndpoint(value = "/serverEndPoint/{userName}")
public class SystemServerEndPoint{
#Autowired
private ServerEndPointService serverEndPointService;
but serverEndPointService is null
How can I inject success
now i tried like this
#Configuration
#ServerEndpoint(value = "/serverEndPoint/{userName}")
public class SystemServerEndPoint{
#Bean
public ServerEndPointService serverEndPointService(){
return new ServerEndPointService();
}
it's ok,serverEndPointService() is not null,but newsMapper in ServerEndPointService is null
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener- class>
</listener>
<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>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>DruidStatView</servlet-name>
<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DruidStatView</servlet-name>
<url-pattern>/druid/*</url-pattern>
</servlet-mapping>
</web-app>
Put these lines:
<annotation-driven />
<context:component-scan base-package="com.ldl.origami.origamiService" />
In your root-context.xml instead of your servlet-context.xml
See if that works.
Related
I am trying to deploy my api + react frontend and managed to serve the built index.html but i am having trouble accessing the /static/** resources shown as so:
This is my webapp structure:
(ignore the index.html over the resources dir)
With my web.xml being:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Campus</display-name>
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
ar.edu.campus.webapp.config.WebConfig,
ar.edu.campus.webapp.security.config.WebSecurityConfig
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter>
<filter-name>jersey</filter-name>
<filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
ar.edu.campus.webapp.controllers,
ar.edu.campus.webapp.security.api.exceptionmappers
</param-value>
</init-param>
<init-param>
<param-name>jersey.config.servlet.filter.contextPath</param-name>
<param-value>/</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>
org.glassfish.jersey.media.multipart.MultiPartFeature,
</param-value>
</init-param>
<init-param>
<param-name>jersey.config.servlet.filter.forwardOn404</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>jersey</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<error-page>
<error-code>404</error-code>
<location>index.html</location>
</error-page>
</web-app>
I tried adding a resource handler over my WebConfig as such but had no result:
#EnableAsync
#EnableTransactionManagement
#ComponentScan({"ar.edu.campus.webapp.controllers", "ar.edu.campus.services", "ar.edu.campus.persistence"})
#Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
...
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/static/**")
.addResourceLocations("classpath:/spa-build/static");
registry
.addResourceHandler("/*.js")
.addResourceLocations("classpath:/spa-build/static/js");
registry
.addResourceHandler("/*.css")
.addResourceLocations("classpath:/spa-build/static/css");
registry
.addResourceHandler("/*.json")
.addResourceLocations("classpath:/spa-build");
registry
.addResourceHandler("/*.ico")
.addResourceLocations("classpath:/spa-build");
}
...
}
I can't really tell what's going wrong, tried multiple other things already but to no avail, i still get 404 over any resource i request.
Solved it by adding:
<webResources>
<resource>
<directory>../spa-client/build</directory>
</resource>
</webResources>
under the < configuration > tag for the maven-war-plugin
I wrote a simple application using vaadin.
#Title("PMC")
#Component("pmcVaadin")
#Scope("prototype")
public class PmcUi extends UI {
#Autowired
private ContentLayout contentLayout;
#Autowired
private TabContent tabs;
#Override
protected void init(VaadinRequest request) {
VerticalLayout mainLayout = new VerticalLayout();
// mainLayout.addComponent(contentLayout.getContent());
mainLayout.addComponent(tabs.createTab("Projects", "Developers"));
mainLayout.setSizeFull();
setContent(mainLayout);
}
}
Tabs - my TabContent component, extended from TabSheet.
But when I click on the tab I've got an error:
Communication problem Take note of any unsaved data, and click here or press ESC to continue. *UIDL could not be read from server. Check servlets mappings. Error code: 404*
My web.xml looks like this
<?xml version="1.0"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/pmc-web-context.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>another-pmc-servlet</servlet-name>
<servlet-class>ru.xpoft.vaadin.SpringVaadinServlet</servlet-class>
<init-param>
<param-name>beanName</param-name>
<param-value>pmcVaadin</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>pmc-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>another-pmc-servlet</servlet-name>
<url-pattern>/VAADIN/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>pmc-servlet</servlet-name>
<url-pattern>/JSP/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>/WEB-INF/jsp/*</url-pattern>
</servlet-mapping>
</web-app>
And I get access to the page using URL http://localhost:8080/pmc-web/VAADIN.
In order to resolve the problem I added mapper for /* to vaadin servlet.
<servlet-mapping>
<servlet-name>another-pmc-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
I have following web application structure:
inside addTerminal.jsp I write following:
....
show map
...
function showMap(lat,lng) {
window.open('map.html?lat='+lat+'&lng='+lng, 'map', 'width=600,height=400');
}
....
But as client cannot access to WEB-INF folder I see 404 when click on href
Can you advice workaround how resolve my issue?
I think I can put map.html into webapp folder but I think exist another fix.
Application technology stack:
Tomcat + Hibernate + SpringMVC
web.xml:
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/webContext.xml</param-value>
</context-param>
<!-- Spring MVC -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/webContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>charsetFilter</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>charsetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
You simply need to create a controller that handles requests to map.html.
Something similar:
#RequestMapping("/map.html")
public String popup(#RequestParam("lat") String lat, #RequestParam("lng") String lng){
// your code here...
return "map";
}
You can use the mvc:resources tag to load items you consider resources.
<mvc:resources location="/web-inf/<path to html file>" mapping="<url context in a browser>" cache-period="86400"/>
Reference: http://docs.spring.io/spring/docs/4.1.2.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#mvc-config-static-resources
I want to use a REST call in Spring MVC on my webapp and return a JSON, I've tried following mkyong's tutorial on this.http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/
However it isn't working, when I call
http://localhost:8080/openRepairsClient/jan.peeters#student.kdg.be
I get a 404 error.
This is my code:
#Controller
public class OpenRepairsRest {
#Autowired
private RepairService repairService;
#Autowired
private UserService userService;
#RequestMapping(value = "/openRepairsClient/{username}")
public #ResponseBody
List<Repair> openRepairsInJson(#PathVariable("username") String username) {
List<Repair> openRepairs = null;
try {
openRepairs = repairService.findOpenRepairsByClient((Client) userService.getUser(username));
} catch (UserServiceException ex) {
Logger.getLogger(OpenRepairsRest.class.getName()).log(Level.SEVERE, null, ex);
}
return openRepairs;
}
}
And yes I'm sure the username exists, otherwise I would get a userservice exception.
My web.xml file:
<web-app version="3.1" 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">
<!-- welcome files -->
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<!-- context parameters -->
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/jsf/faces-config.xml</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>facelets.LIBRARIES</param-name>
<param-value>/WEB-INF/mytags.taglib.xml</param-value>
</context-param>
<!-- listeners -->
<listener>
<!-- Required for Faces to kick in -->
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- Load applicationContext in ServletContextListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Frontcontrollers -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/dispatcher-servlet.xml
</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<filter>
<filter-name>Authentication Filter</filter-name>
<filter-class>be.kdg.repaircafe.filters.AuthenticationFilter</filter-class>
</filter>
<!-- Mappings -->
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>be.kdg.repaircafe.servlets.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<filter-mapping>
<filter-name>Authentication Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/LogoutServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/errorpages/error.jsp</location>
</error-pag
e>
Your error is in the url, it appears that you forgot to write the project name and the word rest. Try with this url:
http://localhost:8080/SpringMVC/rest/openRepairsClient/jan.peeters#student.kdg.be
You can try it with this simplified version of your code:
#Controller
public class OpenRepairsRest {
#RequestMapping(value = "/openRepairsClient/{username}")
public #ResponseBody
List<Shop> openRepairsInJson(#PathVariable("username") String username) {
Shop exampleShop = new Shop();
exampleShop.setName("exampleName");
exampleShop.setStaffName(new String[] { username });
List<Shop> openShops = new ArrayList<Shop>();
openShops.add(exampleShop);
return openShops;
}
}
You need to put your app name in url, example: http://localhost:8080/MyApp/openRepairsClient/jan.peeters#student.kdg.be
The listener doesn't respond to my action:
<h:form>
<p:fileUpload mode="simple" fileUploadListener="#{ADD.uploadImage}" auto="true"/>
</h:form>
and here is the backing bean:
#ManagedBean
#ViewScoped
public class TestClass {
public void uploadImage(FileUploadEvent e){
System.out.println("EVENT");
}
}
and here is my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>thresholdSize</param-name>
<param-value>51200</param-value>
</init-param>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>/tmpDir3/</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
The problem is that the method uploadImage doesn't get called.
What is wrong with my code or what is missing?
If you add the #ManagedBean annotation, the name of the instances of your bean will be the name of your class (with a lower-case first letter). In your example, as the name of your class is TestClass, the name of your bean will be testClass.
So, when you write an EL expression that should invoke a method on this bean, you have to write #{testClass.myMethod} (in your case, this will be #{testClass.uploadImage}.
See: http://docs.oracle.com/javaee/6/api/javax/faces/bean/ManagedBean.html